微信可以檢測到用戶截屏行為(Home + Power),並在稍後點擊附加功能按鈕時詢問用戶是否要發送剛才截屏的圖片,這個用戶體驗非常好。於是乎, 我也想著實現這個功能。
在iOS7之前, 如果用戶截屏,系統會自動取消屏幕上的所有 touch 事件,(使用 touchesCancelled:withEvent: 這個方法)那麼我們就可以檢測這個方法的調用,然後加載本地最新圖片再加以判斷來實現我們的目的。但在 iOS 7 之後,截屏不再會取消屏幕的 touch 事件,所以導致了 Snapchat 和 Facebook Poke 之類的應用在 iOS 7 剛發布時依賴於系統這個行為的功能受到影響。
如果不采取任何新措施, 我們可以讓應用啟動後在後台循環檢測相冊內最新一張照片,看它的是否符合截屏的特征。這種方法可行,但這是個笨方法,需要用戶允許你的程序訪問相冊才可以,並且一直在後台循環會消耗更多的系統資源。
當然, 蘋果封閉了一些東西, 肯定也會給你開放其他東西, 不會讓你走上絕路的。
iOS7提供一個嶄新的推送方法:UIApplicationUserDidTakeScreenshotNotification。只要像往常一樣訂閱即可知道什麼時候截圖了。
注意:UIApplicationUserDidTakeScreenshotNotification 將會在截圖完成之後顯示。現在在截圖截取之前無法得到通知。
希望蘋果會在iOS8當中增加 UIApplicationUserWillTakeScreenshotNotification。(只有did, 沒有will顯然不是蘋果的風格...)
下面就寫了個小demo, 檢測用戶截屏, 並且獲取截屏照片, 顯示在右下角。
(需要在真機上運行, 至少, 模擬器上我不知道如何模擬截屏行為(Home + Power), 如果你知道, 還望告知)
源碼git下載鏈接:colin1994/TakeScreenshotTest
//注冊通知
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(userDidTakeScreenshot:)
name:UIApplicationUserDidTakeScreenshotNotification object:nil];執行操作, 也就是實現上面通知對應的響應函數 -- userDidTakeScreenshot
//截屏響應
- (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];
}我這裡的 userDidTakeScreenshot 總共做了3件事
1.打印檢測到截屏
2.獲取截屏圖片。調用[self imageWithScreenshot]; 這裡的imageWithScreenshot是人為截屏, 模擬用戶截屏操作, 獲取截屏圖片。
3.顯示截屏圖片, 以屏幕1/4大小顯示在右下角, 並且加上白色邊框和陰影效果突出顯示。
/**
* 截取當前屏幕
*
* @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];
}