如何拍摄UIView的屏幕截图? [英] How Do I Take a Screen Shot of a UIView?

查看:174
本文介绍了如何拍摄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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆