項目中需求監(jiān)測用戶截屏行為 并生成圖片誘導(dǎo)用戶分享。像飛豬此類APP已經(jīng)實(shí)現(xiàn)分享功能
產(chǎn)品需求是改變用戶系統(tǒng)截圖的本身上的圖片元素打肝,使其截屏?xí)r保存到相冊的那張圖片是我們想讓他保存的模樣赤兴。
后來查資料發(fā)現(xiàn)iOS只提供了
UIApplicationUserDidTakeScreenshotNotification 這個Notification的API Key 來檢測用戶截屏行為 問題是Did表明是在截屏之后才告訴你已經(jīng)截屏了這個狀態(tài) 暫時未提供will的方法
So 只能像飛豬一樣監(jiān)測到截屏之后自己處理一張截圖 提供給用戶是否選擇分享
注冊通知
[[NSNotificationCenter defaultCenter]addObserver:self
selector:@selector(userDidTakeScreenshot:) name:UIApplicationUserDidTakeScreenshotNotification
object:nil];
實(shí)現(xiàn)監(jiān)測方法&生成UI
- (void)userDidTakeScreenshot:(NSNotification *)notification
{
//模擬用戶截屏行為, 獲取所截圖片(并非系統(tǒng)的截圖 系統(tǒng)截圖還是會保存到相冊 這里可以處理我們自己的截圖選擇去保存或者分享)
UIImage *image = [self imageWithScreenshot];
UIToolbar *baseView = [[UIToolbar alloc]initWithFrame:self.view.bounds];
baseView.barStyle = UIBarStyleBlack;
[self.view.window addSubview:baseView];
//添加顯示
UIImageView *imgvPhoto = [[UIImageView alloc]initWithImage:image];
imgvPhoto.frame = CGRectMake(0, 0, self.view.frame.size.width/1.5, self.view.frame.size.height/1.5);
imgvPhoto.center = baseView.center;
//添加邊框
imgvPhoto.layer.borderColor = [[UIColor xingzheBlue] CGColor];
imgvPhoto.layer.borderWidth = 5.0f;
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, imgvPhoto.y + imgvPhoto.height + 20, SCREENWIDTH, 40);
[button setTitle:@"分享至好友" forState:UIControlStateNormal];
[button setTitleColor:[UIColor xingzheBlue] forState:UIControlStateNormal];
[button addTarget:self action:@selector(shareConfig) forControlEvents:UIControlEventTouchUpInside];
[baseView addSubview:button];
[baseView addSubview:imgvPhoto];
}
分享
- (void)shareConfig
{
//分享代碼
}
截取當(dāng)前屏幕window上的元素生成Image
//截取當(dāng)前屏幕
- (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);
}
//返回截取到的圖片
- (UIImage *)imageWithScreenshot
{
NSData *imageData = [self dataWithScreenshotInPNGFormat];
return [UIImage imageWithData:imageData];
}
如果有其他更好地方法來檢測用戶將要截屏的行為方式歡迎交流分享....