我在我的应用程序中截取屏幕截图。我可以截取屏幕截图。
现在我想通过指定x和y坐标来截取屏幕截图。这可能吗?
UIGraphicsBeginImageContext( self.view.bounds.size );
[self.view.layer renderInContext:UIGraphicsGetCurrentContext( )];
UIImage* aImage = UIGraphicsGetImageFromCurrentImageContext( );
答案 0 :(得分:18)
UIGraphicsBeginImageContext(self.view.bounds.size);
CGContextRef c = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(c, 0, -40); // <-- shift everything up by 40px when drawing.
[self.view.layer renderInContext:c];
UIImage* viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
答案 1 :(得分:2)
如果您使用较新的视网膜显示设备,您的代码应使用UIGraphicsBeginImageContextWithOptions而不是UIGraphicsBeginImageContext考虑分辨率:
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size,YES,2.0);
CGContextRef c = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(c, 0, -40); // <-- shift everything up by 40px when drawing.
[self.view.layer renderInContext:c];
UIImage* viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
这将渲染视网膜显示图像上下文。
答案 2 :(得分:0)
做这样的事情,比所有那些复杂的计算更容易
+ (UIImage *)imageWithView:(UIView *)view {
UIGraphicsBeginImageContextWithOptions([view bounds].size, NO, [[UIScreen mainScreen] scale]);
[[view layer] renderInContext:UIGraphicsGetCurrentContext()];
UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return result;
}
答案 3 :(得分:0)
这是Swift版本
[Required]
答案 4 :(得分:0)
swift version
UIGraphicsBeginImageContext(self.view.bounds.size)
let image: CGContextRef = UIGraphicsGetCurrentContext()!
CGContextTranslateCTM(image, 0, -40)
// <-- shift everything up by 40px when drawing.
self.view.layer.renderInContext(image)
let viewImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil)