以编程方式截取特定区域的屏幕截图 [英] Programmatically take a screenshot of specific area

查看:71
本文介绍了以编程方式截取特定区域的屏幕截图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

就像您可以在我的代码中看到的一样,我截屏并将其保存到相册中.

Like you can see in my code, I take a screenshot and save it to the photo album.

//for retina displays
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
    UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, [UIScreen mainScreen].scale);
} else {
    UIGraphicsBeginImageContext(self.view.bounds.size);
}
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);

在一开始,我使用了webview.size而不是self.view.bounds.size,并且由于视图位于0/0,所以它可以正常工作.但是现在我将WebView居中,但图片以给定大小的0/0开始.

At the beginning I used webview.size instead of self.view.bounds.size and it was working properly because the view was located at 0/0. But now I centered the WebView but the pictures is starts at 0/0 for the given size.

如何配置屏幕快照以给定大小从另一个location(例如300/150)开始?

How can I configure that the screenshot starts at another location (e.g. 300/150) for the given size?

或者还有其他方法可以拍摄UIWebView的照片?

Or is there another way to take a picture of an UIWebView?

推荐答案

我解决了问题,但未回答问题:

I solved my problem but didn't answer the question:

我创建了UIView的子类,并将我的UIWebView移到了那里,因此它相对于该子类位于0/0处.然后使用WebView的大小:

I created a subclass of UIView and moved my UIWebView in there, so that it is located 0/0 relative to the subclass. Then use the size of the WebView:

UIGraphicsBeginImageContext(webview.frame.size);

以及上面的其他代码.

现在,您可以将子类移动到所需的每个位置.

Now you can move the subclass to every location you want.

注意:如果您有日期/时间标签之类的标签,则也必须移动它们,否则将无法在图片上看到它们.

Note: If you have labels like date/time label you have to move them too or you won't see them on the picture.

因此创建一个UIView的子类,并移动要成为的每个IBOutlet 在那里的图片上看到的.

So create a subclass of UIView and move every IBOutlet you want to be seen on the picture in there.

问题仍然存在: 那么可以为屏幕的特定区域拍照吗?

The question is still open: So is it possible to take a picture of a specific area of the screen?

亲切的问候. $ h @ rky

Kind Regards. $h@rky

这篇关于以编程方式截取特定区域的屏幕截图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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