Created
May 8, 2012 21:08
-
-
Save linkov/2639299 to your computer and use it in GitHub Desktop.
UIImage Category for efficient thumbnail gen
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cell.imageView.image = [UIImage imageThumbnailNamed:@"example.jpg" width:20]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <UIKit/UIKit.h> | |
@interface UIImage (Thumbnail) | |
+ (UIImage *)imageThumbnailNamed:(NSString *)name width:(int)thumbWidth; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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