Skip to content

Instantly share code, notes, and snippets.

@edwardean
Created August 11, 2015 06:42
Show Gist options
  • Save edwardean/9a18df85e307881ead98 to your computer and use it in GitHub Desktop.
Save edwardean/9a18df85e307881ead98 to your computer and use it in GitHub Desktop.
NSURL *imageUrl;
CGImageSourceRef imageScorce = CGImageSourceCreateWithURL((__bridge CFURLRef)imageUrl, nil);
NSDictionary *options = @{(__bridge NSString *)kCGImageSourceCreateThumbnailFromImageIfAbsent:(id)kCFBooleanTrue,
(__bridge NSString *)kCGImageSourceCreateThumbnailWithTransform:(id)kCFBooleanTrue,
(__bridge NSString *)kCGImageSourceThumbnailMaxPixelSize:@128};
CFRelease(imageScorce);
CGImageRef thumbImageRef = CGImageSourceCreateThumbnailAtIndex(imageScorce, 0, (__bridge CFDictionaryRef)options);
NSURL *targetUrl;
CGImageDestinationRef destinationRef = CGImageDestinationCreateWithURL((__bridge CFURLRef)targetUrl, kUTTypeJPEG, 1, nil);
if (!destinationRef) {
return;
}
NSDictionary *properties = @{(__bridge NSString *)kCGImageDestinationLossyCompressionQuality:@(0.94)};
CGImageDestinationSetProperties(destinationRef, (__bridge CFDictionaryRef)properties);
CGImageDestinationAddImage(destinationRef, thumbImageRef, NULL);
bool ret = CGImageDestinationFinalize(destinationRef);
CFRelease(destinationRef);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment