PhotoKit架構介紹及使用,photokit架構介紹
PhotoKit 是一套比 AssetsLibrary 更新更完整也更高效的ios照片處理庫,對資源的處理跟 AssetsLibrary 有很大的不同。下面簡單介紹下PhotoKit的幾個基本概念
- PHAsset:代表照片庫中的特定資源,PHAsset對象來表示儲存在照片應用程式中或雲端的一個圖片或視頻檔案。為了顯示或編輯asset,我們需要使用PHAsset類的擷取asset來處理它,一個asset對象是不可變的,只包含它所代表的照片或視頻的中繼資料。
- PHFetchOptions:使用PHFetchOptions對象來指定選項,在PHAsset, PHCollection, PHAssetCollection, and PHCollectionList上根據它的對象來檢索照片符合要求的實體。我們可以根據一些key來對要顯示的照片進行排序。下面是支援的key
- PHFetchResult: 表示一系列的資源集合,也可以是相簿的集合。它是一個有序的儲存照片實體的容器。它是一個泛型集合
- PHAssetCollection: 表示一個相簿
- PHImageManager: 用於處理資源的載入,載入圖片的過程帶有緩衝處理,可以通過傳入一個 PHImageRequestOptions 控制資源的輸出尺寸等規格
- PHImageRequestOptions: 控制載入圖片時的一系列參數
下面詳細介紹庫的使用
我們需要匯入Photos.framework
1.PHPhotoLibrary:表示使用者的照片庫,包括Object Storage Service在本地裝置和(如果已啟用)在iCloud照片。我們使用PHPhotoLibrary來對照片庫進行操作。可以添加編輯刪除。
例如使用PHPhotoLibrary來實現拍照功能。
[_mCamera capturePhotoAsJPEGProcessedUpToFilter:_mFilter withCompletionHandler:^(NSData *processedJPEG, NSError *error){ [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{ [[PHAssetCreationRequest creationRequestForAsset] addResourceWithType:PHAssetResourceTypePhoto data:processedJPEG options:nil]; } completionHandler:^(BOOL success, NSError * _Nullable error) { }]; }];
2.PHAssetChangeRequest,PHAssetCollectionChangeRequest,PHAssetCreationRequest,PHCollectionListChangeRequest:你可以在PHPhotoLibrary的change block中建立一個它們的對象來對asset對象進行添加,編輯,刪除
在上面那段代碼中我們展現了PHAssetCreationRequest的添加操作。
[[PHAssetCreationRequest creationRequestForAsset] addResourceWithType:PHAssetResourceTypePhoto data:processedJPEG options:nil];
3.PHAssetCollectionType相簿類型
typedef NS_ENUM(NSInteger, PHAssetCollectionType) { PHAssetCollectionTypeAlbum = 1, PHAssetCollectionTypeSmartAlbum = 2, PHAssetCollectionTypeMoment = 3,} NS_ENUM_AVAILABLE_IOS(8_0);
[PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil]
輸出:
2016-09-29 15:27:59.392 FWLifeApp[1040:36950] Favorites2016-09-29 15:27:59.394 FWLifeApp[1040:36950] Recently Deleted2016-09-29 15:27:59.394 FWLifeApp[1040:36950] Panoramas2016-09-29 15:27:59.394 FWLifeApp[1040:36950] Camera Roll2016-09-29 15:27:59.396 FWLifeApp[1040:36950] Slo-mo2016-09-29 15:27:59.396 FWLifeApp[1040:36950] Screenshots2016-09-29 15:27:59.397 FWLifeApp[1040:36950] Bursts2016-09-29 15:27:59.398 FWLifeApp[1040:36950] Videos2016-09-29 15:27:59.398 FWLifeApp[1040:36950] Selfies2016-09-29 15:27:59.399 FWLifeApp[1040:36950] Hidden2016-09-29 15:27:59.400 FWLifeApp[1040:36950] Time-lapse2016-09-29 15:27:59.400 FWLifeApp[1040:36950] Recently Added
我們可以看出PHAssetCollectionTypeSmartAlbum包含許多相簿集。收藏,最近刪除,最近添加,相機菲林等,而不包含時刻.
NSMutableArray<FWPhotoAlbums *> *mArr = [NSMutableArray array]; PHFetchResult *smartAlbum = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil]; [smartAlbum enumerateObjectsUsingBlock:^(PHAssetCollection * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
//過濾掉最近刪除的,和視頻 if (obj.assetCollectionSubtype != 202 && obj.assetCollectionSubtype < 212) { NSArray <PHAsset *> *assets = [self getAssetsInAssetCollection:obj ascending:NO]; if ([assets count]) { FWPhotoAlbums *pa = [[FWPhotoAlbums alloc] init]; pa.albumName = [self TitleOfAlbumForChinse:obj.localizedTitle]; pa.albumImageCount = [assets count]; pa.firstImageAsset = assets.firstObject; pa.assetCollection = obj; [mArr addObject:pa]; } } }];
我們需要進一步擷取每個相簿集下的所有照片。對照片的creationDate key進行排序
- (PHFetchResult *)fetchAssetsInAssetCollection:(PHAssetCollection *)assetCollection ascending:(BOOL)ascending{ PHFetchOptions *options = [[PHFetchOptions alloc] init]; options.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:ascending]]; PHFetchResult *result = [PHAsset fetchAssetsInAssetCollection:assetCollection options:options]; return result;}- (NSArray <PHAsset *> *)getAssetsInAssetCollection:(PHAssetCollection *)assetCollection ascending:(BOOL)ascending{ NSMutableArray <PHAsset *> *mArr = [NSMutableArray array]; PHFetchResult *result = [self fetchAssetsInAssetCollection:assetCollection ascending:ascending]; [result enumerateObjectsUsingBlock:^(PHAsset * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { if (obj.mediaType == PHAssetMediaTypeImage) { [mArr addObject:obj]; } }]; return mArr;}
擷取圖片
- (void)requestImageForAsset:(PHAsset *)asset size:(CGSize)size resizeMode:(PHImageRequestOptionsResizeMode)resizeMode completion:(void (^)(UIImage *, NSDictionary *))completion{ static PHImageRequestID requestID = -1; CGFloat scale = [UIScreen mainScreen].scale; CGFloat width = MIN(WIDTH, 800); if (requestID >= 1 && size.width/width==scale) { [[PHCachingImageManager defaultManager] cancelImageRequest:requestID]; } PHImageRequestOptions *option = [[PHImageRequestOptions alloc] init]; option.resizeMode = resizeMode; option.networkAccessAllowed = YES; requestID = [[PHCachingImageManager defaultManager] requestImageForAsset:asset targetSize:size contentMode:PHImageContentModeAspectFill options:option resultHandler:^(UIImage * _Nullable image, NSDictionary * _Nullable info) { BOOL downloadFinined = ![[info objectForKey:PHImageCancelledKey] boolValue] && ![info objectForKey:PHImageErrorKey]; if (downloadFinined && completion) { completion(image, info); } }];}