Skip to content

Instantly share code, notes, and snippets.

@linkov
Created May 8, 2012 21:08
Show Gist options
  • Save linkov/2639299 to your computer and use it in GitHub Desktop.
Save linkov/2639299 to your computer and use it in GitHub Desktop.
UIImage Category for efficient thumbnail gen
cell.imageView.image = [UIImage imageThumbnailNamed:@"example.jpg" width:20];
#import <UIKit/UIKit.h>
@interface UIImage (Thumbnail)
+ (UIImage *)imageThumbnailNamed:(NSString *)name width:(int)thumbWidth;
@end
#import <ImageIO/ImageIO.h>
#import "UIImage+Thumbnail.h"
@implementation UIImage (Thumbnail)
+ (UIImage *)imageThumbnailNamed:(NSString *)name width:(int)thumbWidth {
NSString *fname = [[name lastPathComponent] stringByDeletingPathExtension];
NSString *ext = [name pathExtension];
NSURL* url = [[NSBundle mainBundle] URLForResource:fname withExtension:ext];
CGImageSourceRef src = CGImageSourceCreateWithURL((__bridge CFURLRef)url, NULL);
CGFloat scale = [UIScreen mainScreen].scale;
CGFloat w = thumbWidth*scale;
NSDictionary* d =[NSDictionary dictionaryWithObjectsAndKeys:(id)kCFBooleanTrue, kCGImageSourceShouldAllowFloat, (id)kCFBooleanTrue, kCGImageSourceCreateThumbnailWithTransform, (id)kCFBooleanTrue, kCGImageSourceCreateThumbnailFromImageAlways, [NSNumber numberWithInt:(int)w], kCGImageSourceThumbnailMaxPixelSize, nil];
CGImageRef imref =CGImageSourceCreateThumbnailAtIndex(src, 0, (__bridge CFDictionaryRef)d);
return [UIImage imageWithCGImage:imref scale:scale orientation:UIImageOrientationUp];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment