2015-10-29 213 views
2

觀察UIApplicationUserDidTakeScreenshotNotification通知可以通知用戶,如果他們已經採取了屏幕截圖按Home鍵+電源按鈕。問題是我如何獲取新的屏幕截圖圖像?如何獲取屏幕截圖圖片

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

     // How to get screenshot image? 
     }]; 

回答

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

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

+0

正是我要找的。 – tounaobun

+0

但用戶必須接受相機膠捲許可!我建議再次使用屏幕截圖,但在編程的同時,在您的觀察者中。這樣您就可以獲得用戶屏幕截圖的副本,而無需獲得許可。 – fingerup