0
我正在開發一個應用程序,因爲我的工作是使用AssetsLibrary
從照片庫訪問照片。但我現在面臨以下問題:如何閱讀iCloud/iTunes同步的照片
- 當我嘗試訪問突發照片
- 嘗試從iTunes/iCloud的訪問同步的照片。
所以請幫助我如何使用AssetsLibrary
訪問爆裂照片和同步照片。
我正在開發一個應用程序,因爲我的工作是使用AssetsLibrary
從照片庫訪問照片。但我現在面臨以下問題:如何閱讀iCloud/iTunes同步的照片
所以請幫助我如何使用AssetsLibrary
訪問爆裂照片和同步照片。
此方法適用於Photos
框架。
如果你已經得到了PHAsset
,您可以通過以下方式聯繫該資產的數據:
// For image
PHImageRequestOptions *options = [PHImageRequestOptions new];
options.networkAccessAllowed = YES;
options.version = PHImageRequestOptionsVersionCurrent;
options.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat;
//If comes from iCloud, the progressHandler will be called.
options.progressHandler = ^(double progress, NSError *__nullable error, BOOL *stop, NSDictionary *__nullable info) {
NSLog(@"percent:%f, info:%@", progress, info);
// Handle progress
};
[[PHImageManager defaultManager] requestImageDataForAsset:asset options:options resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, UIImageOrientation orientation, NSDictionary * _Nullable info) {
if (imageData) {
// Handle data
}
}];
// For video
PHVideoRequestOptions *options = [PHVideoRequestOptions new];
options.version = PHVideoRequestOptionsVersionCurrent;
options.deliveryMode = PHVideoRequestOptionsDeliveryModeHighQualityFormat;
options.progressHandler = ^(double progress, NSError * _Nullable error, BOOL * _Nonnull stop, NSDictionary * _Nullable info) {
NSLog(@"percent:%f, info:%@", progress, info);
// Handle progress
};
[[PHImageManager defaultManager] requestAVAssetForVideo:asset options:options resultHandler:^(AVAsset * _Nullable asset, AVAudioMix * _Nullable audioMix, NSDictionary * _Nullable info) {
AVURLAsset *avurlasset = (AVURLAsset*)asset;
// Handle data
}];
獲取資產:
PHFetchResult *userLibrary = [PHAssetCollection
fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum
subtype:PHAssetCollectionSubtypeSmartAlbumUserLibrary
options:nil];
[userLibrary enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
PHAssetCollection *assetCollection = (PHAssetCollection *)obj;
PHFetchResult *fetchResult = [self getFetchResultInAssetCollection:assetCollection];
[fetchResult enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
PHAsset *asset = (PHAsset *)obj;
// Handle asset
}];
}];
-getFetchResultInAssetCollection:
-(PHFetchResult *)getFetchResultInAssetCollection:(PHAssetCollection *)assetCollection {
PHFetchOptions *options = [[PHFetchOptions alloc] init];
// video and image
options.predicate = [NSPredicate predicateWithFormat:@"mediaType = %d || mediaType = %d", PHAssetMediaTypeImage, PHAssetMediaTypeVideo];
// Sort by creationDate
options.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:NO]];
PHFetchResult *fetchResult = [PHAsset fetchAssetsInAssetCollection:assetCollection options:options];
return fetchResult;
}
感謝@湯姆,但我想用AssetLibrary來做這個,不是照片框架 – venkat
@venkat OK ,https://github.com/donobono/DoImagePickerController可能對你有所幫助。 – tomfriwel
@venkat此存儲庫使用'AssetHelper'中的'AssetLibrary'來處理照片。 – tomfriwel