2010-06-19 30 views

回答

7

像这样:

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

您将需要包括CoreGraphics中框架,并导入CALayer.h:

#import <QuartzCore/CALayer.h> 
+0

正是我在找的东西! – 2011-01-25 00:26:23

+0

我知道这是旧的,但对于Retina显示器使用'UIGraphicsBeginImageContextWithOptions(myView.bounds.size,myView.opaque,0.0);'以避免看起来模糊的图片。 – Jakar 2014-05-01 22:55:29

2

这里,试试这个

CGImageRef screen = UIGetScreenImage(); 
UIImage *screenImage = [UIImage imageWithCGImage:screen]; 

这将需要在屏幕的截图,所以理论上捕捉所有视图的元素,给你一个UIImage去工作。希望有所帮助!

+0

嘿,这很酷,但问题是它捕捉整个主视图,我有一个标签栏,例如在我的主要竞争对手在最终的UIImage中,我不想要任何方法来解决这个问题? 在此先感谢, -David – bobbypage 2010-06-19 06:30:01

+0

您可以尝试裁剪UIImage到您需要的确切x,y坐标。如果这对你来说不是一个好选择,那么这种方法可能不起作用 – 2010-06-19 06:32:28

+0

你也许可以在捕获图像的阶段使元素不可见? – KTOV 2017-02-25 14:30:59

0

添加下面的方法来UIView的类别和使用

- (UIImage*) capture { 
    UIGraphicsBeginImageContext(self.bounds.size); 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    [self.layer renderInContext:context]; 
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    return img; 
} 
相关问题