Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get UIImage from view's layers?

I have a view which has sublayers whose contents are images.

I was hoping to get the view's image by myView.image but apparently the view doesn't have image just layers.

How do I create a UIImage from view's layers?

like image 646
eugene Avatar asked Feb 15 '11 10:02

eugene


2 Answers

UIGraphicsBeginImageContext(yourView.bounds.size);
[yourView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

Note: you need to include QuartzCore for this.

like image 186
Max Avatar answered Nov 02 '22 11:11

Max


CGRect myRect =CGRectMake(0, 0, 300, 300);// [self.view bounds];
UIGraphicsBeginImageContext(myRect.size);

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

[self.view.layer renderInContext:ctx];
UIImage *image1 = UIGraphicsGetImageFromCurrentImageContext();
like image 1
dhaya Avatar answered Nov 02 '22 11:11

dhaya