Created
October 16, 2012 02:10
-
-
Save lightory/3896901 to your computer and use it in GitHub Desktop.
UIViewAdditions
This file contains 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
#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