IOS 檢測截圖並獲取截圖圖片(原理介紹)

之前用Swift給大家分享過一篇截圖分享的文章:https://www.jianshu.com/p/51ae56f1eb63。現在給大家用OC代碼小小的翻譯一下。

先簡單介紹一下,大家使用“支付寶”等APP的時候,只要截圖,頁面就會彈出提示。那麼APP是如何知道我們進行了截圖的操作呢?這種牛逼哄哄的效果其實很簡單,因爲在iOS7以後蘋果爲大家提供了一個獲取截圖的通知,會在截圖後調用:

OC:UIApplicationUserWillTakeScreenshotNotification
Swift:UIApplicationUserDidTakeScreenshot

其實有了通知,接下來就很簡單了。有的同學會考慮獲得截圖的通知後調用系統的相冊,並且獲取最後一張圖片,這個方法可以實現,不過有很多缺點。實現成本先不用說,萬一用戶不同意獲取相冊豈不是傻眼了嗎。所以,最簡單的方法就是直接截取當前屏幕的圖片,既簡單又方便,上代碼:

#import "ViewController.h"

@interface ViewController ()

/// 添加裁剪顯示的圖片
@property (nonatomic, strong) UIImageView *imageView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupViewController];
}

-(void)setupViewController {
    /// 隨便一張默認的佔位圖(同學們不需要的話可以直接刪除)
    UIImageView *testImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image"]];
    testImageView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
    [self.view addSubview:testImageView];
    
    /// 添加裁剪顯示的圖片
    self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 200, 200, 300)];
    self.imageView.contentMode = UIViewContentModeScaleAspectFit;
    self.imageView.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:self.imageView];
    
    /// 添加通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(userDidTakeScreenshot) name:UIApplicationUserDidTakeScreenshotNotification object:nil];
}

-(void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark - 截屏通知
-(void)userDidTakeScreenshot {
    self.imageView.image = [self takeScreenshot];
}

#pragma mark - 截取當前屏幕
-(UIImage *)takeScreenshot {
    CGSize imageSize = CGSizeZero;
    CGSize screenSize = [UIScreen mainScreen].bounds.size;
    UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;

    if (UIInterfaceOrientationIsPortrait(orientation)) {
        imageSize = screenSize;
    } else {
        imageSize = CGSizeMake(screenSize.height, screenSize.width);
    }
    
    UIGraphicsBeginImageContextWithOptions(imageSize, false, 0);
    CGContextRef context = UIGraphicsGetCurrentContext();
    if (context) {
        for (UIWindow *window in [UIApplication sharedApplication].windows) {
            CGContextSaveGState(context);
            CGContextTranslateCTM(context, window.center.x, window.center.y);
            CGContextConcatCTM(context, window.transform);
            CGContextTranslateCTM(context, -window.bounds.size.width * window.layer.anchorPoint.x, -window.bounds.size.height * window.layer.anchorPoint.y);
            
            if (orientation == UIInterfaceOrientationLandscapeLeft) {
                CGContextRotateCTM(context, M_PI_4);
                CGContextTranslateCTM(context, 0, -imageSize.width);
            } else if (orientation == UIInterfaceOrientationLandscapeRight) {
                CGContextRotateCTM(context, - M_PI_2);
                CGContextTranslateCTM(context, -imageSize.height, 0);
            } else if (orientation == UIInterfaceOrientationPortraitUpsideDown) {
                CGContextRotateCTM(context, M_PI);
                CGContextTranslateCTM(context, -imageSize.width, -imageSize.height);
            }
            
            if ([window respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)]) {
                [window drawViewHierarchyInRect:window.bounds afterScreenUpdates:YES];
            } else {
                [window.layer renderInContext:context];
            }
            
            CGContextRestoreGState(context);
        }
    }
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return image;
}

@end

最後還是需要提醒一下各位同學,使用時建議大家將方法放置到Base控制器,這樣就可以方便的在APP的任意位置獲取截圖,並且統一分享。喜歡的同學可以點一個贊哈。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章