CGImageSourceCreateWithURL with authentication

▼魔方 西西 提交于 2020-01-05 04:59:28

问题


i'm trying to read image property from a url like :

http://www.example.com/ws/img/8

As it's a webservice, i need to pass authentication to URL. I don't want to download the image to read it's properties. So i'm trying to use CGImageSourceCreateWithURL like this:

CGImageSourceRef imageSourceRef = CGImageSourceCreateWithURL((__bridge CFURLRef)[NSURL URLWithString:@"http://admin:admin@www.example.com/ws/img/8"], NULL);
CFDictionaryRef props = CGImageSourceCopyPropertiesAtIndex(imageSourceRef, 0, NULL);

But props still return null.

Is there a way to access image properties with authentication? (If i download image through RestKit function, it works of course)

Thanks


回答1:


Rather than using CGImageSourceCreateWithURL, I would use an NSURLRequest with the appropriate authentication and download the image as an NSData and then use CGImageSourceCreateWithData, or just download the image with RestKit if that already works.

This will allow you a lot more flexibility in authenticate protocols. The withURL initializer is really just a utility function if you're doing a basic HTTP GET without added information required.




回答2:


I think you'd need to look at using CGImageSourceCreateWithDataProvider and using the CGDataProvider to download only the requested information from the image. This would allow you to use an API for the download which gave you the option of adding the Authorization header.

If you have control of the server it would be better all round to create an index file which gives you details of the images that are available and what size they are. You're obviously getting the image URLs from somewhere so it should also return some contextual data at the same time.



来源:https://stackoverflow.com/questions/18852013/cgimagesourcecreatewithurl-with-authentication

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!