3

Observing UIApplicationUserDidTakeScreenshotNotification notification can notify users if they have taken a screenshot pressing home button + power button.The question is how can I fetch the newly screenshot image?

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

         // How to get screenshot image?
      }];
tounaobun
  • 14,570
  • 9
  • 53
  • 75

1 Answers1

2

Using this to get the recent image:

    // 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);
                                 }];

Ref: How to retrieve the most recent photo from Camera Roll on iOS?

Community
  • 1
  • 1
tuledev
  • 10,177
  • 4
  • 29
  • 49
  • Exactly what I'm looking for. – tounaobun Oct 29 '15 at 07:33
  • 1
    But the user must accept Camera Roll permission !! I suggest to take again a screenshot, but programmatically at same time, in your observer. That way you have a copy of user screenshot, without asking permission. – fingerup May 11 '16 at 08:36