iOS檢測用戶截屏並獲取所截圖片


iOS檢測用戶截屏並獲取所截圖片

微信可以檢測到用戶截屏行為(Home + Power),並在稍后點擊附加功能按鈕時詢問用戶是否要發送剛才截屏的圖片,這個用戶體驗非常好。
在iOS7之前, 如果用戶截屏,系統會自動取消屏幕上的所有 touch 事件,(使用 touchesCancelled:withEvent : 這個方法)
那么我們就可以檢測這個方法的調用,然后加載本地最新圖片再加以判斷來實現我們的目的。
但在 iOS 7 之后,截屏不再會取消屏幕的 touch 事件,所以導致了 Snapchat 和 Facebook Poke 之類的應用在 iOS 7 剛發布時依賴於系統這個行為的功能受到影響。

另外種方法是應用啟動后在后台循環檢測相冊內最新一張照片,看它的是否符合截屏的特征。這種方法可行,但這是個笨方法,需要用戶允許你的程序訪問相冊才可以,並且一直在后台循環會消耗更多的系統資源。Github 上有一個開源代碼做了這個功能。我使用 Instruments 檢測在 iPhone 4S 、 iOS 6.1.3 的環境下 CPU 占用為 %2。
https://github.com/clayallsopp/ShotBlocker

iOS7提供一個新的推送方法:
UIApplicationUserDidTakeScreenshotNotification
只要像往常一樣訂閱即可知道什么時候截圖了。
注意:UIApplicationUserDidTakeScreenshotNotification
將會在截圖完成之后顯示
現在在截圖截取之前無法得到通知。
希望蘋果會在iOS8當中增加 UIApplicationUserWillTakeScreenshotNotification。

=========================
下面demo, 檢測用戶截屏, 並且獲取截屏照片, 顯示在右下角。
(需要在真機上運行, 模擬器上不知道如何模擬截屏行為(Home + Power))

一、注冊通知:

[[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];
}

 

=============================

如何阻止iOS應用檢測截屏行為?
app有檢測截屏行為,截屏超過三次即鎖賬戶。
如果不想讓app檢測到自己截屏有辦法破解嗎?知道方法的請回復留言。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM