微信可以檢測(cè)到用戶截屏行為(Home + Power)茬暇,并在稍后點(diǎn)擊附加功能按鈕時(shí)詢問用戶是否要發(fā)送剛才截屏的圖片燃观,這個(gè)用戶體驗(yàn)非常好织阅。于是乎, 我也想著實(shí)現(xiàn)這個(gè)功能拟蜻。
在iOS7之前, 如果用戶截屏绎签,系統(tǒng)會(huì)自動(dòng)取消屏幕上的所有 touch 事件,(使用 touchesCancelled:withEvent : 這個(gè)方法)那么我們就可以檢測(cè)這個(gè)方法的調(diào)用酝锅,然后加載本地最新圖片再加以判斷來實(shí)現(xiàn)我們的目的诡必。但在 iOS 7 之后,截屏不再會(huì)取消屏幕的 touch 事件搔扁,所以導(dǎo)致了 Snapchat 和 Facebook Poke 之類的應(yīng)用在 iOS 7 剛發(fā)布時(shí)依賴于系統(tǒng)這個(gè)行為的功能受到影響爸舒。
如果不采取任何新措施, 我們可以讓應(yīng)用啟動(dòng)后在后臺(tái)循環(huán)檢測(cè)相冊(cè)內(nèi)最新一張照片,看它的是否符合截屏的特征稿蹲。這種方法可行扭勉,但這是個(gè)笨方法,需要用戶允許你的程序訪問相冊(cè)才可以场绿,并且一直在后臺(tái)循環(huán)會(huì)消耗更多的系統(tǒng)資源剖效。
當(dāng)然, 蘋果封閉了一些東西, 肯定也會(huì)給你開放其他東西, 不會(huì)讓你走上絕路的嫉入。
iOS7提供一個(gè)嶄新的推送方法:
UIApplicationUserDidTakeScreenshotNotification
焰盗。只要像往常一樣訂閱即可知道什么時(shí)候截圖了。
注意:UIApplicationUserDidTakeScreenshotNotification
將會(huì)在截圖完成之后顯示
≈淞郑現(xiàn)在在截圖截取之前無法得到通知熬拒。
希望蘋果會(huì)在iOS8當(dāng)中增加 UIApplicationUserWillTakeScreenshotNotification。(只有did, 沒有will顯然不是蘋果的風(fēng)格...)
下面就寫了個(gè)小demo, 檢測(cè)用戶截屏, 并且獲取截屏照片, 顯示在右下角垫竞。
(需要在真機(jī)上運(yùn)行, 至少, 模擬器上我不知道如何模擬截屏行為(Home + Power), 如果你知道, 還望告知)
源碼git下載鏈接:colin1994/TakeScreenshotTest
一澎粟。注冊(cè)通知:
//注冊(cè)通知
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(userDidTakeScreenshot:)
name:UIApplicationUserDidTakeScreenshotNotification object:nil];
二蛀序。監(jiān)聽截屏:
執(zhí)行操作, 也就是實(shí)現(xiàn)上面通知對(duì)應(yīng)的響應(yīng)函數(shù)? -- userDidTakeScreenshot
//截屏響應(yīng)
- (void)userDidTakeScreenshot:(NSNotification *)notification
{
NSLog(@"檢測(cè)到截屏");
//人為截屏, 模擬用戶截屏行為, 獲取所截圖片
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;
//添加四個(gè)邊陰影
imgvPhoto.layer.shadowColor = [UIColor blackColor].CGColor;
imgvPhoto.layer.shadowOffset = CGSizeMake(0, 0);
imgvPhoto.layer.shadowOpacity = 0.5;
imgvPhoto.layer.shadowRadius = 10.0;
//添加兩個(gè)邊陰影
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];
}
我這里的 userDidTakeScreenshot 總共做了3件事
1.打印檢測(cè)到截屏
2.獲取截屏圖片。調(diào)用 [self imageWithScreenshot]; 這里的imageWithScreenshot是人為截屏, 模擬用戶截屏操作, 獲取截屏圖片活烙。
3.顯示截屏圖片, 以屏幕1/4大小顯示在右下角, 并且加上白色邊框和陰影效果突出顯示徐裸。
三。獲取截屏圖片
/**
*? 截取當(dāng)前屏幕
*
*? @return NSData *
*/
- (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);
}
/**
*? 返回截取到的圖片
*
*? @return UIImage *
*/
- (UIImage *)imageWithScreenshot
{
NSData *imageData = [self dataWithScreenshotInPNGFormat];
return [UIImage imageWithData:imageData];
}