1

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

Taylan Aydinli
  • 4,333
  • 15
  • 39
  • 33
ApheX
  • 685
  • 2
  • 10
  • 26

2 Answers2

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.

Ben S
  • 68,394
  • 30
  • 171
  • 212
  • Hi Benoit. As i say, i don't want to download image to read it's properties. Why? Because I use collectionview and I need to get image size in `(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath` before downloading image in individual collectionview cell. – ApheX Sep 17 '13 at 14:18
  • CGImageSourceCreateWithURL would actually download the image in order to read its properties. – Ben S Sep 17 '13 at 14:31
  • Hmmm ok, i did not understand how this method work so... There is no way to read image properties without downloading it? – ApheX Sep 17 '13 at 14:34
  • `CGImageSourceCreateWithURL` should not load the pixel data, it will only load the image properties. That's the whole point of the method. – Wain Sep 17 '13 at 15:36
  • 1
    I guess it could depend on the server as to what options are supported and how much of the data would actually be downloaded... – Wain Sep 17 '13 at 15:43
0

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.

Wain
  • 118,658
  • 15
  • 128
  • 151