Skip to content

Instantly share code, notes, and snippets.

@genkernel
Created August 3, 2012 15:15
Show Gist options
  • Save genkernel/3248548 to your computer and use it in GitHub Desktop.
Save genkernel/3248548 to your computer and use it in GitHub Desktop.
iOS. Load image downsampled to specified max pixel size.
+ (UIImage *)thumbnailImageFromURL:(NSURL*)imageUrl downsampledToMaxPixelSize:(NSNumber *)maxPixelSize {
NSDictionary *opts = @{
(id)kCGImageSourceThumbnailMaxPixelSize: maxPixelSize,
(id)kCGImageSourceCreateThumbnailWithTransform: (id)kCFBooleanTrue,
(id)kCGImageSourceCreateThumbnailFromImageIfAbsent: (id)kCFBooleanTrue
};
CGImageSourceRef src = CGImageSourceCreateWithURL((__bridge CFURLRef)imageUrl, NULL);
CGImageRef thumbnail = CGImageSourceCreateThumbnailAtIndex(src, 0, (__bridge CFDictionaryRef)opts);
UIImage *img = [UIImage imageWithCGImage:thumbnail];
CGImageRelease(thumbnail);
CFRelease(src);
return img;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment