Skip to content

Instantly share code, notes, and snippets.

@jeksys
Created October 25, 2012 16:09
Show Gist options
  • Save jeksys/3953709 to your computer and use it in GitHub Desktop.
Save jeksys/3953709 to your computer and use it in GitHub Desktop.
Render a layer to an image with scalefactor
dispatch_async(dispatch_get_main_queue(), ^{
previewToSave = nil;
CGSize screenSize = self.view.bounds.size;
float scaleFactor = 0.2;
const size_t originalWidth = screenSize.width * scaleFactor;
const size_t originalHeight = screenSize.height * scaleFactor;
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
CGContextRef ctx = CGBitmapContextCreate(nil, originalWidth, originalHeight, 8, 4*(int)originalWidth, colorSpaceRef, kCGImageAlphaPremultipliedLast);
if (ctx != nil) {
CGContextSetShouldAntialias(ctx, NO);
CGContextSetAllowsAntialiasing(ctx, NO);
CGContextSetInterpolationQuality(ctx, kCGInterpolationMedium);
CGContextTranslateCTM(ctx, 0, originalHeight);
CGContextScaleCTM(ctx, scaleFactor, -1 * scaleFactor);
[aView.layer renderInContext:ctx];
CGImageRef cgImage = CGBitmapContextCreateImage(ctx);
CGContextRelease(ctx);
CGColorSpaceRelease(colorSpaceRef);
previewToSave = [UIImage imageWithCGImage:cgImage];
NSLog(@"%d, %d, %d", (int)previewToSave.size.width, (int)previewToSave.size.height, [UIImagePNGRepresentation(previewToSave) length]/1024);
CGImageRelease(cgImage);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment