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

?著作權歸作者所有,轉載或內容合作請聯系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容