以编程方式截取特定区域的屏幕截图

问题描述:

就像您在我的代码中看到的那样,我截取了屏幕截图并将其保存到相册中.

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