Skip to content

Instantly share code, notes, and snippets.

@vitoziv
Created July 4, 2014 06:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vitoziv/96a08ee9bf8c515eecf5 to your computer and use it in GitHub Desktop.
Save vitoziv/96a08ee9bf8c515eecf5 to your computer and use it in GitHub Desktop.
Resize Image With low memory
+ (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