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