iOS捕捉屏幕截屏事件

来源:互联网 发布:李小龙身材数据 编辑:程序博客网 时间:2024/04/29 12:50

iOS7 提供了截屏的通知,如下:

    NSOperationQueue *mainQueue = [NSOperationQueue mainQueue];    [[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationUserDidTakeScreenshotNotification                                                      object:nil                                                       queue:mainQueue                                                  usingBlock:^(NSNotification *note) {                                                      // executes after screenshot                                                                                                            NSLog(@"截屏事件发生");                                                                                                        }];

UIApplicationUserDidTakeScreenshotNotification 只可以在iOS7 以上可以使用。
参考这里:http://stackoverflow.com/questions/13484516/ios-detection-of-screenshot

iOS7以后的系统,可以通过系统提供的API来实现截屏功能:

- (UIView *)snapshotViewAfterScreenUpdates:(BOOL)afterUpdates NS_AVAILABLE_IOS(7_0);

对于iOS7以前的系统,可以通过以下的代码实现截屏的功能:

- (UIImage *)captureImageFromView:(UIView *)view{    CGRect screenRect = [view bounds];    UIGraphicsBeginImageContext(screenRect.size);    CGContextRef ctx = UIGraphicsGetCurrentContext();    [view.layer renderInContext:ctx];    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();    UIGraphicsEndImageContext();    return image;}


0 0
原创粉丝点击