Created
July 4, 2014 06:22
-
-
Save vitoziv/96a08ee9bf8c515eecf5 to your computer and use it in GitHub Desktop.
Resize Image With low memory
This file contains hidden or 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
+ (UIImage *)resizeImage:(UIImage *)image scaledToFitSize:(CGFloat)max | |
{ | |
NSData *imageData = [NSData dataWithData:UIImageJPEGRepresentation(image, 1.0)]; | |
CGImageSourceRef imageSource = CGImageSourceCreateWithData((__bridge CFDataRef)imageData, NULL); | |
if (!imageSource) | |
return nil; | |
CFDictionaryRef options = (__bridge CFDictionaryRef)[NSDictionary dictionaryWithObjectsAndKeys: | |
(id)kCFBooleanTrue, (id)kCGImageSourceCreateThumbnailWithTransform, | |
(id)kCFBooleanTrue, (id)kCGImageSourceCreateThumbnailFromImageIfAbsent, | |
(id)[NSNumber numberWithFloat:max], (id)kCGImageSourceThumbnailMaxPixelSize, | |
nil]; | |
CGImageRef imgRef = CGImageSourceCreateThumbnailAtIndex(imageSource, 0, options); | |
UIImage* scaled = [UIImage imageWithCGImage:imgRef]; | |
CGImageRelease(imgRef); | |
CFRelease(imageSource); | |
return scaled; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment