Skip to content

Instantly share code, notes, and snippets.

@Koze
Last active November 30, 2022 22:12
Show Gist options
  • Save Koze/c93a7a951a7ee1d1cb80 to your computer and use it in GitHub Desktop.
Save Koze/c93a7a951a7ee1d1cb80 to your computer and use it in GitHub Desktop.
Getting All Screenshots with Photos.framework
NSMutableArray *mArray = [NSMutableArray array];
// fetch all image assets
PHFetchOptions *fetchOptions = [[PHFetchOptions alloc] init];
fetchOptions.predicate = [NSPredicate predicateWithFormat:@"mediaType == %d", PHAssetMediaTypeImage];
PHFetchResult *result = [PHAsset fetchAssetsWithOptions:fetchOptions];
[result enumerateObjectsUsingBlock:^(PHAsset * __nonnull asset, NSUInteger idx, BOOL * __nonnull stop) {
// filter with subtype for screenshot
if (asset.mediaSubtypes & PHAssetMediaSubtypePhotoScreenshot) {
[mArray addObject:asset];
}
}];
// ex. retrieve image data
PHAsset *asset = result.firstObject;
PHImageRequestOptions *options = [[PHImageRequestOptions alloc] init];
options.synchronous = YES;
[[PHImageManager defaultManager] requestImageDataForAsset:asset options:options resultHandler:^(NSData * __nullable imageData, NSString * __nullable dataUTI, UIImageOrientation orientation, NSDictionary * __nullable info) {
UIImage *image = [UIImage imageWithData:imageData];
NSLog(@"%@", image);
// stop at this point with breakpoint, you can see quicklook
}];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment