Skip to content

Instantly share code, notes, and snippets.

@Blankwonder
Last active March 30, 2022 12:07
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save Blankwonder/b9a576eca49af115adaf70c4ebb27986 to your computer and use it in GitHub Desktop.
Save Blankwonder/b9a576eca49af115adaf70c4ebb27986 to your computer and use it in GitHub Desktop.
@import Photos;
[PHPhotoLibrary requestAuthorizationForAccessLevel:PHAccessLevelReadWrite handler:^(PHAuthorizationStatus status) {
NSLog(@"PHAuthorizationStatus: %ld", status);
PHFetchOptions *fetchOptions = [[PHFetchOptions alloc] init];
fetchOptions.includeHiddenAssets = YES;
fetchOptions.includeAllBurstAssets = YES;
PHFetchResult<PHAsset *> *result = [PHAsset fetchAssetsWithOptions:fetchOptions];
PHImageManager *manager = [PHImageManager defaultManager];
PHImageRequestOptions *options = [[PHImageRequestOptions alloc] init];
options.networkAccessAllowed = YES;
PHVideoRequestOptions *voptions = [[PHVideoRequestOptions alloc] init];
voptions.networkAccessAllowed = YES;
__block NSInteger counter = 0;
__block NSInteger total = 0;
for (PHAsset *asset in result) {
total++;
void (^block)(BOOL success, NSDictionary *info) = ^void(BOOL success, NSDictionary *info) {
if (!success) {
NSLog(@"Failed: %@ %@", asset, info);
}
counter++;
if (counter == total) {
NSLog(@"Completed");
}
};
if (asset.mediaType == PHAssetMediaTypeImage) {
[manager requestImageDataAndOrientationForAsset:asset options:options resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, CGImagePropertyOrientation orientation, NSDictionary * _Nullable info) {
block(imageData != nil, info);
}];
} else if (asset.mediaType == PHAssetMediaTypeVideo) {
[manager requestPlayerItemForVideo:asset options:voptions resultHandler:^(AVPlayerItem * _Nullable playerItem, NSDictionary * _Nullable info) {
block(playerItem != nil, info);
}];
} else {
NSLog(@"Unknown type: %@", asset);
}
}
NSLog(@"Total: %ld", total);
}];
@Arthraim
Copy link

苦涩

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment