[英]How to capture the contents of a UIView and change it into a UIImage?
我目前有一个视图,我想将其更改为UIImage。 我想这样做是因为UIImage类对于我需要做的事情要好得多。 你如何捕获UIView的内容并将内容复制到UIImage中?
谢谢,
-大卫
像这样:
UIGraphicsBeginImageContext(myView.bounds.size);
[myView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *myImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
您需要包含CoreGraphics框架,并导入CALayer.h:
#import <QuartzCore/CALayer.h>
在这里,试试这个
CGImageRef screen = UIGetScreenImage();
UIImage *screenImage = [UIImage imageWithCGImage:screen];
这将截取屏幕截图,因此理论上捕获所有视图的元素,并为您提供UIImage。 希望有所帮助!
将以下方法添加到UIView类别并使用
- (UIImage*) capture {
UIGraphicsBeginImageContext(self.bounds.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[self.layer renderInContext:context];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.