集團考勤最新的意見反饋需求,參照了京東的截屏反饋。
重點就是如何監聽到 用戶觸發了系統級的截屏,并獲取到當前截屏圖片。
監聽到用戶截屏后,有兩種處理方式:
- 方式一:模擬用戶的截屏動作,用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];
}