OC版本
- (UIImage*)screenSnapshot:(UIView *)view{
UIGraphicsBeginImageContextWithOptions(view.frame.size, NO, 0);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImage * iii = [[UIImage alloc] initWithData: UIImageJPEGRepresentation(image, 0.8)];
return iii;
}
swift版本
//截取屏幕
func screenSnapshot(view: UIView) -> UIImage? {
UIGraphicsBeginImageContextWithOptions(view.frame.size, false, 0.0)
view.layer.render(in: UIGraphicsGetCurrentContext()!)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
//截取指定范围
return UIImage.init(data: image?.jpegData(compressionQuality: 0.5) ?? Data()) //image
}
//截取整个屏幕
func screenSnapScreen() -> UIImage? {
//截取整个屏幕
guard let window = UIApplication.shared.keyWindow else { return nil }
// 用下面这行而不是UIGraphicsBeginImageContext(),因为前者支持Retina
UIGraphicsBeginImageContextWithOptions(window.bounds.size, false, 0.0)
window.layer.render(in: UIGraphicsGetCurrentContext()!)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return UIImage.init(data: image?.jpegData(compressionQuality: 0.5) ?? Data()) //image
}
网友评论