ios截屏

作者: best_su | 来源:发表于2019-07-31 15:22 被阅读0次

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

相关文章

  • flutter:截屏

    1.flutter-截屏组件 2.flutter-截屏插件 3.flutter-iOS原生截屏 iOS代码 4.获...

  • (最新)iOS截屏

    ios webview 截屏:ios截屏 前言:介绍一下截屏有很多种做法1:截当前屏幕内容2:截整个视图的所有内容...

  • ios截屏

    ios截屏

  • iOS 应用内截屏分享

    需求:捕获用户截屏操作,并建议用户截屏后的操作。虽然iOS11 有系统的截屏,但 APP 内截屏可便捷操作。 封装...

  • iOS 截屏&长截屏

    截屏在 iOS 开发中经常用到,本篇文章讲的是监听用户截屏操作,并且获取截屏图片,如果当前是UIScrollVie...

  • iOS屏幕截图功能

    iOS7.0之前的系统,可以通过以下代码实现截屏功能。 iOS7.0之后,系统中封装了截屏的方法- (UIView...

  • iOS截屏

    1. 一句代码截屏 2. UIGraphics 3. 还有就是之前一个大佬写的给webview截长图的 其实就是利...

  • iOS 截屏

    最后调用:UIImage *capturedImage = [img captureView]; 注释的是把.m中...

  • IOS 截屏

  • iOS截屏

    现在我们只需要一行代码就可以完成上述步骤

网友评论

      本文标题:ios截屏

      本文链接:https://www.haomeiwen.com/subject/usktdctx.html