Reputation: 13409
So far I have this method:
+ (CGSize) imageDimensions: (NSString *)url {
NSURL *imageFileURL = [NSURL URLWithString: url];
CGImageSourceRef imageSource = CGImageSourceCreateWithURL((__bridge CFURLRef)imageFileURL, NULL);
if (imageSource == NULL) {
return CGSizeMake(0, 0);
}
CGFloat width = 0.0f, height = 0.0f;
CFDictionaryRef imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, NULL);
if (imageProperties != NULL) {
CFNumberRef widthNum = CFDictionaryGetValue(imageProperties, kCGImagePropertyPixelWidth);
if (widthNum != NULL) {
CFNumberGetValue(widthNum, kCFNumberFloatType, &width);
}
CFNumberRef heightNum = CFDictionaryGetValue(imageProperties, kCGImagePropertyPixelHeight);
if (heightNum != NULL) {
CFNumberGetValue(heightNum, kCFNumberFloatType, &height);
}
CFRelease(imageProperties);
}
return CGSizeMake(width, height);
}
However I was doing some tests and it seems that this method only runs as fast as if I were to download the entire image and check the size property. I had thought this method worked by just downloading the image's meta data or headers and checking the resolution there. Am I wrong? Is there a quick way to get image dimensions from an internet image in objective C as quick as possible without grabbing the image?
Upvotes: 3
Views: 561
Reputation: 61331
Most graphical formats have their dimension stored in the header. If you limit yourself to a subset of formats (PNG/JPG/GIF, for example), and implement your own header parsing code, then it's possible. You'll be on your own, though - the Core Graphics image APIs do not work with partial files.
To request a part of the file from the 'Net, you can use the Range
header. Some Web servers might not honor it, though. Request the number of bytes that's maximum among header sizes of supported formats. Also, you might want to validate the magic bytes in the header.
Upvotes: 2