原文地址:http://blog.csdn.net/hitwhylz/article/details/38386979?utm_source=tuicool&utm_medium=referral
之前看淘寶有,截屏后彈出截取圖片(Home + Power),可以分享給好友這種功能。就上網(wǎng)搜了一下踢械,感謝大神的分享。
(在iOS7之前, 如果用戶(hù)截屏魄藕,系統(tǒng)會(huì)自動(dòng)取消屏幕上的所有 touch 事件内列,(使用 touchesCancelled:withEvent: 這個(gè)方法)那么我們就可以檢測(cè)這個(gè)方法的調(diào)用,然后加載本地最新圖片再加以判斷來(lái)實(shí)現(xiàn)我們的目的泼疑。但在 iOS 7 之后德绿,截屏不再會(huì)取消屏幕的 touch 事件,所以導(dǎo)致了 Snapchat 和 Facebook Poke 之類(lèi)的應(yīng)用在 iOS 7 剛發(fā)布時(shí)依賴(lài)于系統(tǒng)這個(gè)行為的功能受到影響退渗。
如果不采取任何新措施, 我們可以讓?xiě)?yīng)用啟動(dòng)后在后臺(tái)循環(huán)檢測(cè)相冊(cè)內(nèi)最新一張照片移稳,看它的是否符合截屏的特征。這種方法可行会油,但這是個(gè)笨方法个粱,需要用戶(hù)允許你的程序訪問(wèn)相冊(cè)才可以,并且一直在后臺(tái)循環(huán)會(huì)消耗更多的系統(tǒng)資源翻翩。
當(dāng)然, 蘋(píng)果封閉了一些東西, 肯定也會(huì)給你開(kāi)放其他東西, 不會(huì)讓你走上絕路的都许。
iOS7提供一個(gè)嶄新的推送方法:UIApplicationUserDidTakeScreenshotNotification。只要像往常一樣訂閱即可知道什么時(shí)候截圖了嫂冻。
注意:UIApplicationUserDidTakeScreenshotNotification 將會(huì)在截圖完成之后顯示〗赫鳎現(xiàn)在在截圖截取之前無(wú)法得到通知。
希望蘋(píng)果會(huì)在iOS8當(dāng)中增加 UIApplicationUserWillTakeScreenshotNotification桨仿。(只有did, 沒(méi)有will顯然不是蘋(píng)果的風(fēng)格...)
下面就寫(xiě)了個(gè)小demo, 檢測(cè)用戶(hù)截屏, 并且獲取截屏照片, 顯示在右下角睛低。
(需要在真機(jī)上運(yùn)行, 至少, 模擬器上我不知道如何模擬截屏行為(Home + Power), 如果你知道, 還望告知))
可以直接看代碼
一。注冊(cè)通知:
[objc] view plain copy
//注冊(cè)通知
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(userDidTakeScreenshot:)
name:UIApplicationUserDidTakeScreenshotNotification object:nil];
二服傍。監(jiān)聽(tīng)截屏:
執(zhí)行操作, 也就是實(shí)現(xiàn)上面通知對(duì)應(yīng)的響應(yīng)函數(shù)? -- userDidTakeScreenshot
[objc] view plain copy
//截屏響應(yīng)
- (void)userDidTakeScreenshot:(NSNotification *)notification
{
NSLog(@"檢測(cè)到截屏");
//人為截屏, 模擬用戶(hù)截屏行為, 獲取所截圖片
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是人為截屏, 模擬用戶(hù)截屏操作, 獲取截屏圖片。
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];
}