eugene
eugene

Reputation: 41785

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?

Upvotes: 8

Views: 7885

Answers (2)

dhaya
dhaya

Reputation: 1522

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();

Upvotes: 1

Max
Max

Reputation: 16709

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

Note: you need to include QuartzCore for this.

Upvotes: 21

Related Questions