iOS 監聽用戶截屏并獲取

集團考勤最新的意見反饋需求,參照了京東的截屏反饋。
重點就是如何監聽到 用戶觸發了系統級的截屏,并獲取到當前截屏圖片。

監聽到用戶截屏后,有兩種處理方式:

  • 方式一:模擬用戶的截屏動作,用layer 去繪制當前app的window 展示的內容,然后進行圖像處理(比較復雜),獲得UIImage
  • 方式二:在截屏后,去訪問用戶相冊,拿到用戶相冊的最后一張圖片,判斷是不是截屏,然后采用。

一、注冊監聽通知 -- userDidTakeScreenshot

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(userDidTakeScreenshot:)                                              
name:UIApplicationUserDidTakeScreenshotNotification object:nil];

二、監聽用戶截屏動作,并處理layer(方式一)

-(void)userDidTakeScreenshot:(NSNotification *)notification
{
    NSLog(@"檢測到截屏");
    //人為截屏, 模擬用戶截屏行為, 獲取所截圖片
    UIImage *image_ = [self imageWithScreenshot];
    //添加顯示
    UIImageView *imgvPhoto = [[UIImageView alloc]initWithImage:image_];
    imgvPhoto.frame = CGRectMake(self.window.frame.size.width/2, self.window.frame.size.height/2, self.window.frame.size.width/2, self.window.frame.size.height/2);
    //添加邊框
    CALayer * layer = [imgvPhoto layer];
    layer.borderColor = [
                         [UIColor whiteColor] CGColor];
    layer.borderWidth = 5.0f;
    //添加四個邊陰影
    imgvPhoto.layer.shadowColor = [UIColor blackColor].CGColor;
    imgvPhoto.layer.shadowOffset = CGSizeMake(0, 0);
    imgvPhoto.layer.shadowOpacity = 0.5;
    imgvPhoto.layer.shadowRadius = 10.0;
    //添加兩個邊陰影
    imgvPhoto.layer.shadowColor = [UIColor blackColor].CGColor;
    imgvPhoto.layer.shadowOffset = CGSizeMake(4, 4);
    imgvPhoto.layer.shadowOpacity = 0.5;
    imgvPhoto.layer.shadowRadius = 2.0;
    [self.window addSubview:imgvPhoto];
}

三、處理截屏圖像(方式一)

- (UIImage *)imageWithScreenshot
{
    NSData *imageData = [self dataWithScreenshotInPNGFormat];
    return [UIImage imageWithData:imageData];
}
- (NSData *)dataWithScreenshotInPNGFormat
{
    CGSize imageSize = CGSizeZero;
    UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
    if (UIInterfaceOrientationIsPortrait(orientation))
        imageSize = [UIScreen mainScreen].bounds.size;
    else
        imageSize = CGSizeMake([UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width);

    UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);
    CGContextRef context = UIGraphicsGetCurrentContext();
    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_2);
            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 UIImagePNGRepresentation(image);
}

四、方式二:獲取用戶最后一張圖片

static NSTimeInterval const latestAssetFetchInterval = 10;

@interface AJUserPhotoFetchManager ()

@property (nonatomic, strong, nullable) NSDate *lastAssetcreationDate;
@end

static NSString * const kLastAssetcreationDateKey = @"kLastAssetcreationDateKey";
@implementation AJUserPhotoFetchManager

#pragma mark - 單例方法
static AJUserPhotoFetchManager *sharedInstance;
+ (id)allocWithZone:(struct _NSZone *)zone
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [super allocWithZone:zone];
        sharedInstance.lastAssetcreationDate = [[NSUserDefaults standardUserDefaults] objectForKey:kLastAssetcreationDateKey];
    });
    
    return sharedInstance;
}

+ (AJUserPhotoFetchManager *)sharedUserPhotoFetchManager
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[AJUserPhotoFetchManager alloc]init];
        sharedInstance.lastAssetcreationDate = [[NSUserDefaults standardUserDefaults] objectForKey:kLastAssetcreationDateKey];
    });
    return sharedInstance;
}


/** 點擊“+”號的時候獲取相冊列表,獲取最新保存的一張圖片。
  * 根據圖片保存時間,與當前時間戳進行計算,獲得間隔時間。從而判斷是否是需求的時間間隔。(時間間隔自定義)
 */
- (void)fetchLatestPhotoInTimeIntervalWithCompletion:(void (^)(UIImage *result, NSDictionary *info))completion{
    // 此處不能主動獲取權限,在用戶同意的情況下可以去獲取
    PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
    if (status == PHAuthorizationStatusAuthorized) {
        
//        AJUserPhotoFetchManager *sharedUserPhotoFetchManager = [AJUserPhotoFetchManager sharedUserPhotoFetchManager];
        PHAsset *latestAsset = [self fetchLatestPhotoAsset];
        NSDate *nowDate = [NSDate date];
        NSTimeInterval timeInterval = [nowDate timeIntervalSinceDate:latestAsset.creationDate];// 創建時間距離的時間間隔
        if (timeInterval > latestAssetFetchInterval) { // 超出時間了
            return;
        }
        // 對一張圖片10s內兩次獲取,雖然是同一張圖片,系統回調的圖片結果地址不一致。 
        // 閱讀了相關博客也不建議用回調的info里面字段作判斷, 所以這里采用的圖片的時間戳
        if (self.lastAssetcreationDate && [self.lastAssetcreationDate compare:latestAsset.creationDate] != NSOrderedAscending) { // 上次已經獲取過了
            return;
        }
        
        PHImageRequestOptions *options = [[PHImageRequestOptions alloc] init];
        options.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat;
        
        [[PHImageManager defaultManager] requestImageForAsset:latestAsset targetSize:UIScreen.mainScreen.bounds.size contentMode:PHImageContentModeAspectFit options:options resultHandler:^(UIImage *result, NSDictionary *info) {
            self.lastAssetcreationDate = latestAsset.creationDate;
            [[NSUserDefaults standardUserDefaults] setObject:latestAsset.creationDate forKey:kLastAssetcreationDateKey];
            completion(result, info);
        }];
    }
}

- (PHAsset *)fetchLatestPhotoAsset{

    PHFetchOptions *options = [[PHFetchOptions alloc]init];
    if (@available(iOS 9.0, *)) {
        options.fetchLimit = 1;
    }
    options.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:NO]];
    PHFetchResult *assetsFetchResults = [PHAsset fetchAssetsWithOptions:options];
    return assetsFetchResults.firstObject;
}

+ (void)requestAuthorization:(void (^)(PHAuthorizationStatus))handler{
    [PHPhotoLibrary requestAuthorization:handler];
}
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 微信可以檢測到用戶截屏行為(Home + Power),并在稍后點擊附加功能按鈕時詢問用戶是否要發送剛才截屏的圖片...
    像羽毛那樣輕閱讀 2,558評論 0 6
  • Swift1> Swift和OC的區別1.1> Swift沒有地址/指針的概念1.2> 泛型1.3> 類型嚴謹 對...
    cosWriter閱讀 11,136評論 1 32
  • 最近感覺日更碼字特別艱難,想想是否也去讀點有關寫作的書呢?于是翻到了美國作家娜塔莉·戈德堡的這本《寫出我...
    Sisy陽閱讀 220評論 0 4
  • 第十章---論工資與利潤隨勞動與資本用途不同而不同 首先討論用途不同而產生差異的種類: 職業本身有令人愉快與不快者...
    個革馬閱讀 8,134評論 0 9
  • 就讓靈魂失重 就讓生命暗潮洶涌 你是遠方的星辰 世界為你蒼白失色 你的瞳孔折射出的華光溢彩 是夏娃的蘋果 靈魂為你...
    仙人f閱讀 201評論 4 7