Sha*_*rky 16 screenshot objective-c area uiwebview
就像你在我的代码中看到的那样,我会截取屏幕截图并将其保存到相册中.
//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);
Run Code Online (Sandbox Code Playgroud)
一开始我使用webview.size而不是self.view.bounds.size它,它工作正常,因为视图位于0/0.但现在我将WebView集中在一起0/0,但图片是从给定大小开始的.
如何配置屏幕截图从另一个location(例如300/150)开始给定大小?
还是有另一种方式拍摄一张照片UIWebView吗?
esc*_*ord 22
您需要进行两项更改才能获取部分屏幕:
之后,您只需将图层渲染到上下文中,就可以得到您想要的内容.像这样的东西应该这样做:
CGRect grabRect = CGRectMake(40,40,300,200);
//for retina displays
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
UIGraphicsBeginImageContextWithOptions(grabRect.size, NO, [UIScreen mainScreen].scale);
} else {
UIGraphicsBeginImageContext(grabRect.size);
}
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(ctx, -grabRect.origin.x, -grabRect.origin.y);
[self.view.layer renderInContext:ctx];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);
Run Code Online (Sandbox Code Playgroud)
idS*_*tar 10
我已经采取并测试了上面的@escrafford的答案,并让它工作得很好.我在UIView上将它作为类别方法的上下文中进行了测试,因此'grabRect'可以是参数化的.这是代码,作为返回UIImageView的UIView类别:
/**
Takes a screenshot of a UIView at a specific point and size, as denoted by
the provided croppingRect parameter. Returns a UIImageView of this cropped
region.
CREDIT: This is based on @escrafford's answer at http://stackoverflow.com/a/15304222/535054
*/
- (UIImageView *)rp_screenshotImageViewWithCroppingRect:(CGRect)croppingRect {
// For dealing with Retina displays as well as non-Retina, we need to check
// the scale factor, if it is available. Note that we use the size of teh cropping Rect
// passed in, and not the size of the view we are taking a screenshot of.
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
UIGraphicsBeginImageContextWithOptions(croppingRect.size, YES, [UIScreen mainScreen].scale);
} else {
UIGraphicsBeginImageContext(croppingRect.size);
}
// Create a graphics context and translate it the view we want to crop so
// that even in grabbing (0,0), that origin point now represents the actual
// cropping origin desired:
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(ctx, -croppingRect.origin.x, -croppingRect.origin.y);
[self.layer renderInContext:ctx];
// Retrieve a UIImage from the current image context:
UIImage *snapshotImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// Return the image in a UIImageView:
return [[UIImageView alloc] initWithImage:snapshotImage];
}
Run Code Online (Sandbox Code Playgroud)
我解决了我的问题,但没有回答问题:
我创建了 的子类UIView并将其移至UIWebView其中,以便它相对于子类位于 0/0 位置。然后使用WebView的大小:
UIGraphicsBeginImageContext(webview.frame.size);
Run Code Online (Sandbox Code Playgroud)
以及上面的其他代码。
现在您可以将子类移动到您想要的每个位置。
注意:如果您有日期/时间标签等标签,您也必须移动它们,否则您不会在图片上看到它们。
因此,创建一个子类
UIView并移动IBOutlet您希望在其中的图片上看到的所有内容。
问题仍然悬而未决:那么是否可以拍摄屏幕特定区域的照片?
亲切的问候。$h@rky