监听用户截屏

作者: MrSong | 来源:发表于2017-06-01 10:34 被阅读28次

    监听用户截屏

    @()

    这些小技巧必须要记下来,以备不时之需。

    • iOS8之前可以用 ALAssetsLibrary

    • iOS8 以后可以用 PHAsset

    • 监听通知 UIApplicationUserDidTakeScreenshotNotification

    - iOS8之前可以用 ALAssetsLibrary

    #pragma mark - 获取最新截图
    /*
     #import <AssetsLibrary/AssetsLibrary.h>
     */
    - (void)latestAsset:(void (^)(UIImage * _Nullable, NSError *_Nullable))block {
        if (SystemVersion < 8.0) {
            
            [self enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
                if (group) {
                    [group setAssetsFilter:[ALAssetsFilter allPhotos]];
                    [group enumerateAssetsWithOptions:NSEnumerationReverse/*遍历方式,逆序*/ usingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
                        if (result) {
                            ALAssetRepresentation *repr = [result defaultRepresentation];
                            UIImage *img = [UIImage imageWithCGImage:[repr fullResolutionImage]];
                            *stop = YES;
                            if (block) {
                                block(img,nil);
                            }
                            *stop = YES;
                        }
                        
                    }];
                    *stop = YES;
                }
            } failureBlock:^(NSError *error) {
                if (error) {
                    if (block) {
                        block(nil,error);
                    }
                }
            }];
        }
    }
    
    

    iOS8 以后可以用 PHAsset

    /*
    #import <Photos/PHAsset.h>
    #import <Photos/PHImageManager.h>
    #import <Photos/PHFetchOptions.h>
     */
     /// 获取最新截图
    - (void)latestPhoto:(void (^_Nullable)(UIImage * _Nullable))block  {
        
        if (SystemVersion >= 8.0) {
            
            PHFetchOptions *fetchOptions = [[PHFetchOptions alloc] init];
            fetchOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:NO]];
            
            PHImageRequestOptions * requestOptions = [[PHImageRequestOptions alloc] init];
            requestOptions.synchronous = YES;
            
            PHFetchResult<PHAsset*> * fetchResult = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:fetchOptions];
            
            if (fetchResult.count > 0) {
                PHAsset *asset = [fetchResult objectAtIndex:0];
                PHImageManager * manager = [PHImageManager defaultManager];
                CGSize targetSize = CGSizeMake(asset.pixelWidth, asset.pixelHeight);
                [manager requestImageForAsset:asset
                                   targetSize:targetSize
                                  contentMode:PHImageContentModeAspectFit
                                      options:requestOptions
                                resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
                                    
                                    if (result) {
                                        if(block) {
                                            block(result);
                                        }
                                    }
                                    
                                }];
            }
        }
    }
    
    

    相关文章

      网友评论

        本文标题:监听用户截屏

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