如何捕获UIView的内容并将其更改为UIImage?
问题描述:
我目前有一个视图,我想将其更改为UIImage。我想这样做是因为UIImage类更适合我需要做的事情。你将如何捕获UIView的内容并将其内容复制到UIImage中?如何捕获UIView的内容并将其更改为UIImage?
感谢,
-David
答
像这样:
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;
}
正是我在找的东西! – 2011-01-25 00:26:23
我知道这是旧的,但对于Retina显示器使用'UIGraphicsBeginImageContextWithOptions(myView.bounds.size,myView.opaque,0.0);'以避免看起来模糊的图片。 – Jakar 2014-05-01 22:55:29