ios截屏
#import "UIView+SCScreenShot.h"
#import <Photos/Photos.h>
@implementation UIView (SCScreenShot)
- (void)screenShot_writeImageToPhone: (CGSize)size {
UIImageWriteToSavedPhotosAlbum([self screenShot:size], self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}
- (UIImage *)screenShot: (CGSize)size {
// 这种方法是截取layer上的图,有时候截取view上的时候会有问题
// UIGraphicsBeginImageContextWithOptions(size, YES, [[UIScreen mainScreen] scale]);
//
// [self.layer renderInContext:UIGraphicsGetCurrentContext()];
// UIImage *screenShotImage = UIGraphicsGetImageFromCurrentImageContext();
// UIGraphicsEndImageContext();
// 这个是截取View上的图
UIGraphicsBeginImageContextWithOptions(size, YES, 0.0);
[self drawViewHierarchyInRect:self.bounds afterScreenUpdates:NO];
UIImage *screenShotImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return screenShotImage;
}
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self animated:YES];
hud.mode = MBProgressHUDModeText;
if ([PHPhotoLibrary authorizationStatus] == PHAuthorizationStatusNotDetermined) {
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
if (status == PHAuthorizationStatusAuthorized) {
NSLog(@"点同意");
} else if (status == PHAuthorizationStatusDenied || status == PHAuthorizationStatusRestricted) {
NSLog(@"点拒绝");
}
}];
} else if ([PHPhotoLibrary authorizationStatus] == PHAuthorizationStatusAuthorized) {
if (error != nil) {
hud.labelText = @"图片保存失败";
[hud hide:YES afterDelay:1.0];
} else {
hud.labelText = @"图片保存成功";
[hud hide:YES afterDelay:1.0];
}
} else {
hud.labelText = @"相册未授权, 请打开相册权限";
[hud hide:YES afterDelay:1.0];
}
}
@end
网友评论