Last active
November 30, 2022 22:12
-
-
Save Koze/c93a7a951a7ee1d1cb80 to your computer and use it in GitHub Desktop.
Getting All Screenshots with Photos.framework
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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