天天看點

iOS 截屏監聽

蘋果有提供方法,可以注冊監聽,當使用者有截屏操作的時候會通知我們:

1、注冊:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didTakeScreenshot:) name:UIApplicationUserDidTakeScreenshotNotification object:nil];

2、回調:

- (void)didTakeScreenshot:(NSNotification *)notification {

}

3、再次一并獻上截屏資料擷取

- (NSData*)screenshotWithRect:(CGRect)rect{

    UIView *view = [UIApplication sharedApplication].windows[0].rootViewController.view;

    UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, [UIScreen mainScreen].scale);

    [view.layer renderInContext:UIGraphicsGetCurrentContext()];

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return UIImagePNGRepresentation(image);

}