【发布时间】:2011-06-27 12:43:52
【问题描述】:
我有一个视图,其中包含内容为图像的子层。
我希望通过 myView.image 获取视图的图像,但显然视图没有图像,只有图层。
如何从视图层创建 UIImage?
【问题讨论】:
我有一个视图,其中包含内容为图像的子层。
我希望通过 myView.image 获取视图的图像,但显然视图没有图像,只有图层。
如何从视图层创建 UIImage?
【问题讨论】:
UIGraphicsBeginImageContext(yourView.bounds.size);
[yourView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
注意:您需要为此包含 QuartzCore。
【讨论】:
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();
【讨论】: