ios截屏

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

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容