News

Here you can find some of the latest news from the mobile design and development world.

Code snippet: how to get a screenshot of a UIView

10 February

Here’s something that could be useful if you’re trying to find a simple way of passing any UIView (and its subviews) as a UIImage – literally taking a screenshot of whatever is on that view:

Found and slightly adapted from here

[objc]
+ (UIImage *)captureView:(UIView *)view withArea:(CGRect)screenRect {

UIGraphicsBeginImageContext(screenRect.size);

CGContextRef ctx = UIGraphicsGetCurrentContext();
[[UIColor blackColor] set];
CGContextFillRect(ctx, screenRect);

[view.layer renderInContext:ctx];

UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return newImage;
}
[/objc]

Tags: , , , , ,