Skip to content

Instantly share code, notes, and snippets.

@lightory
Created October 16, 2012 02:10
Show Gist options
  • Save lightory/3896901 to your computer and use it in GitHub Desktop.
Save lightory/3896901 to your computer and use it in GitHub Desktop.
UIViewAdditions
#import "UIViewAdditions.h"
CGContextRef createBitmapContext(int pixelsWide, int pixelsHigh)
{
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGBitmapInfo bitmapInfo = (kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst);
CGContextRef bitmapContext = CGBitmapContextCreate(nil, pixelsWide, pixelsHigh, 8, 0, colorSpace, bitmapInfo);
CGColorSpaceRelease(colorSpace);
return bitmapContext;
}
@implementation UIView (UIViewAdditions)
-(UIImage *)asImage {
CGSize screenShotSize = self.bounds.size;
//CGSize screenShotSize = [self getFrameOfScrollView].size;
CGContextRef contextRef = createBitmapContext(screenShotSize.width, screenShotSize.height);
CGContextTranslateCTM(contextRef, 0, screenShotSize.height);
CGContextScaleCTM(contextRef, 1, -1);
[self.layer renderInContext:contextRef];
CGImageRef imageRef = CGBitmapContextCreateImage(contextRef);
CGContextRelease(contextRef);
UIImage * img = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
// return the image
return img;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment