1>截图
- (UIImage *)captureCurrentView:(UIView *)view {
CGRect frame = view.frame;
// 下面的方法截图不清晰
// UIGraphicsBeginImageContext(frame.size);
// 使用此方法可以解决不清晰的问题
UIGraphicsBeginImageContextWithOptions(frame.size, false, UIScreen.main.scale)
CGContextRef contextRef = UIGraphicsGetCurrentContext();
[view.layer renderInContext:contextRef];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
2> 保存图片
-(void)saveImageToPhotos:(UIImage *)image {
/*
使用这个方法时我们可以看到头文件让我们实现
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo;
方法
*/
UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSaveWithError:contextInfo:),nil);
}
3>回调
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
if (error == nil) {
NSLog(@"保存成功");
} else {
NSLog(@"失败");
}
}
4>使用Swift注意点:Swift由于是直接将OC转化成Swift语法的,所以注释没有提供相应的回调方法.这时候我们需要自己转化一下.
func image(image: UIImage, didFinishSavingWithError: NSError?,contextInfo: AnyObject) {
if didFinishSavingWithError != nil {
debugPrint("保存失败")
} else {
debugPrint("保存成功")
}
}