Last active
December 31, 2024 11:32
-
-
Save Blankwonder/b9a576eca49af115adaf70c4ebb27986 to your computer and use it in GitHub Desktop.
This file contains 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
@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
苦涩