如何截取 UIView 的屏幕截图? [英] How Do I Take a Screen Shot of a UIView?
问题描述
我想知道我的 iPhone 应用程序如何将特定 UIView
的屏幕截图作为 UIImage
.
I am wondering how my iPhone app can take a screen shot of a specific UIView
as a UIImage
.
我尝试了这段代码,但得到的只是一张空白图像.
I tried this code but all I get is a blank image.
UIGraphicsBeginImageContext(CGSizeMake(320,480));
CGContextRef context = UIGraphicsGetCurrentContext();
[myUIView.layer drawInContext:context];
UIImage *screenShot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
myUIView
的尺寸为 320x480,并且有一些子视图.这样做的正确方法是什么?
myUIView
has dimensions 320x480 an it has some sub-views.
What is the correct way to do this?
推荐答案
我想你可能想要 renderInContext
,而不是 drawInContext
.drawInContext 更像是一个你会覆盖的方法......
I think you may want renderInContext
, not drawInContext
. drawInContext is more a method you would override...
请注意,它可能不适用于所有视图,特别是大约一年前,当我尝试将其与实时摄像机视图一起使用时,它不起作用.
Note that it may not work in all views, specifically a year or so ago when I tried to use this with the live camera view it did not work.
这篇关于如何截取 UIView 的屏幕截图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!