Skip to content

Instantly share code, notes, and snippets.

@ymoussaba
Created February 27, 2016 23:09
Show Gist options
  • Select an option

  • Save ymoussaba/83e90d95085d37ed4275 to your computer and use it in GitHub Desktop.

Select an option

Save ymoussaba/83e90d95085d37ed4275 to your computer and use it in GitHub Desktop.
iOS: How to retrieve image dimensions without loading CGImage into memory
// This method requires ImageIO.framework
#import <ImageIO/ImageIO.h>
- (CGSize)sizeOfImageAtURL:(NSURL *)imageURL
{
// With CGImageSource we avoid loading the whole image into memory
CGSize imageSize = CGSizeZero;
CGImageSourceRef source = CGImageSourceCreateWithURL((CFURLRef)imageURL, NULL);
if (source) {
NSDictionary *options = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO] forKey:(NSString *)kCGImageSourceShouldCache];
NSDictionary *properties = (NSDictionary *)CGImageSourceCopyPropertiesAtIndex(source, 0, (CFDictionaryRef)options);
if (properties) {
NSNumber *width = [properties objectForKey:(NSString *)kCGImagePropertyPixelWidth];
NSNumber *height = [properties objectForKey:(NSString *)kCGImagePropertyPixelHeight];
if ((width != nil) && (height != nil))
imageSize = CGSizeMake(width.floatValue, height.floatValue);
CFRelease(properties);
}
CFRelease(source);
}
return imageSize;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment