Skip to content

Instantly share code, notes, and snippets.

@rijieli
Forked from Blankwonder/FixStuckCloudPhoto.m
Created March 30, 2022 12:07
Show Gist options
  • Save rijieli/dd5457c42bae4a4a995bd24c30c390fd to your computer and use it in GitHub Desktop.
Save rijieli/dd5457c42bae4a4a995bd24c30c390fd 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);
}];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment