Last active
September 19, 2017 07:09
-
-
Save kimjj81/5ad58c96fad37396913d9708d2e8f2de to your computer and use it in GitHub Desktop.
resize uiimage
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
void CGImageWriteToFile(CGImageRef image, NSString *path) { | |
CFURLRef url = (__bridge CFURLRef) [NSURL fileURLWithPath:path]; | |
CGImageDestinationRef destination = CGImageDestinationCreateWithURL(url, kUTTypePNG, 1, NULL); | |
CGImageDestinationAddImage(destination, image, nil); | |
if (!CGImageDestinationFinalize(destination)) { | |
NSLog(@"Failed to write image to %@", path); | |
} | |
} |
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
//http://pulkitgoyal.in/resizing-high-resolution-images-on-ios-without-memory-issues/ | |
- (void)resizeImageAtPath:(NSString *)imagePath { | |
// Create the image source (from path) | |
CGImageSourceRef src = CGImageSourceCreateWithURL((__bridge CFURLRef) [NSURL fileURLWithPath:imagePath], NULL); | |
// To create image source from UIImage, use this | |
// NSData* pngData = UIImagePNGRepresentation(image); | |
// CGImageSourceRef src = CGImageSourceCreateWithData((CFDataRef)pngData, NULL); | |
// Create thumbnail options | |
CFDictionaryRef options = (__bridge CFDictionaryRef) @{ | |
(id) kCGImageSourceCreateThumbnailWithTransform : @YES, | |
(id) kCGImageSourceCreateThumbnailFromImageAlways : @YES, | |
(id) kCGImageSourceThumbnailMaxPixelSize : @(640) | |
}; | |
// Generate the thumbnail | |
CGImageRef thumbnail = CGImageSourceCreateThumbnailAtIndex(src, 0, options); | |
CFRelease(src); | |
// Write the thumbnail at path | |
CGImageWriteToFile(thumbnail, imagePath); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment