如何获取屏幕截图图片

3

观察 UIApplicationUserDidTakeScreenshotNotification 通知可以通知用户是否通过按下主页按钮+电源按钮来截屏。问题是如何获取新的截屏图像?

// observe screenshot notification
NSOperationQueue *mainQueue = [NSOperationQueue mainQueue];
[[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationUserDidTakeScreenshotNotification
          object:nil
           queue:mainQueue
      usingBlock:^(NSNotification *note) {

         // How to get screenshot image?
      }];
1个回答

2

使用以下方法获取最新的图片:

    // How to get screenshot image?
    ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc] init];
    [assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos
                                 usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
                                     if (nil != group) {
                                         // be sure to filter the group so you only get photos
                                         [group setAssetsFilter:[ALAssetsFilter allPhotos]];

                                         if (group.numberOfAssets > 0) {
                                             [group enumerateAssetsAtIndexes:[NSIndexSet indexSetWithIndex:group.numberOfAssets - 1]
                                                                     options:0
                                                                  usingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
                                                                      if (nil != result) {
                                                                          ALAssetRepresentation *repr = [result defaultRepresentation];
                                                                          // this is the most recent saved photo
                                                                          UIImage *img = [UIImage imageWithCGImage:[repr fullResolutionImage]];
                                                                          // we only need the first (most recent) photo -- stop the enumeration
                                                                          *stop = YES;
                                                                      }
                                                                  }];
                                         }
                                     }

                                     *stop = NO;
                                 } failureBlock:^(NSError *error) {
                                     NSLog(@"error: %@", error);
                                 }];

参考:如何从iOS相机胶卷中检索最新的照片?


正是我所寻找的。 - tounaobun
1
但是用户必须接受相机权限!! 我建议在您的观察器中以编程方式同时再次截取屏幕截图。这样,您就可以拥有用户屏幕截图的副本,而无需请求权限。 - fingerup

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接