Capture view contents into an UIImage object

By combining UIGraphicsBeginImageContext() and -renderInContext in CALayer, you can draw contents into a image (UIImage) object. It is like screen capture.

[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

CGContextTranslateCTM() modifies the x- and y- coordinates (otherwise it starts from (0,0)). By using CGContextTranslateCTM(), you can change the start capturing point so that it can capture limited area in the UIScrollView content.

CGSize pageSize = CGSizeMake(320, 480);
int currentPage = 2;
CGContextRef resizedContext = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(resizedContext, 320 * currentPage, 0);
[scrollView.layer renderInContext:resizedContext];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

Capture UIImage with offset into UIScrollView

, , , , ,

No comments yet.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.