본문 바로가기

iPhone dev./UIView, UITableView

UIView를 UIImage로 캡쳐하기

예제)

#import <QuartzCore/QuartzCore.h>
 
- (UIImage*)captureView:(UIView *)view {
	CGRect rect = [[UIScreen mainScreen] bounds];	 
	UIGraphicsBeginImageContext(rect.size);	 
	CGContextRef context = UIGraphicsGetCurrentContext();	 
	[view.layer renderInContext:context];	 
	UIImage *img = UIGraphicsGetImageFromCurrentImageContext();	 
	UIGraphicsEndImageContext();	 
	return img;
}
 
- (void)saveScreenshotToPhotosAlbum:(UIView *)view {
	UIImageWriteToSavedPhotosAlbum([self captureView:view], nil, nil, nil);
}

-> Returns a UIImage that contains a render of any UIView(except EAGLView).
If you pass your app's UIWindow into this method, it will return you a screenshot of the app.