轉(zhuǎn):http://blog.csdn.net/icetime17/article/details/49745055
PhotoKit是蘋果推出的用于替代ALAssetsLibrary的框架。
PhotoKit為使用照片和視頻資源提供了新的API,包括由Photos app管理的iCloud Photos資產(chǎn),所以你可以在“Camera roll”中直接編輯照片,無需提前導(dǎo)入。PhotoKit還包含一個線程安全架構(gòu)用以獲取、緩存縮略圖和全尺寸圖片,請求資產(chǎn)更改,遵守其他應(yīng)用所做的變化,以及對資產(chǎn)內(nèi)容進(jìn)行可恢復(fù)的編輯。
正因?yàn)樘O果已經(jīng)做了大量的封裝工作,使得開發(fā)者在使用PhotoKit的時候非常方便。在這里,將對比之前的一篇ALAssetsLibrary學(xué)習(xí)記錄來簡單介紹PhotoKit的基本使用。先簡單回顧下ALAssetsLibrary:
ALAssetsLibrary 提供了訪問iOS設(shè)備下照片應(yīng)用下所有照片和視頻的接口.從 ALAssetsLibrary 中可讀取所有的相冊數(shù)據(jù),即 ALAssetsGroup 對象列表; 從每個 ALAssetsGroup 中可獲取到其中包含的照片或視頻列表,即 ALAsset 對象列表. 其層次關(guān)系為
ALAssetsLibrary -> ALAssetsGroup -> ALAsset -> ALAssetRepresentation.
每個 ALAsset 可能有多個representations表示, 即ALAssetRepresentation 對象:
使用其defaultRepresentation 方法可獲得其默認(rèn)representations,
使用[asset valueForProperty: ALAssetPropertyRepresentations ]可獲取其所有representations的 UTI 數(shù)組。
從ALAsset對象可獲取縮略圖 thumbnail 或 aspectRatioThumbnail ;
需要注意的是:
通過ALAssetsLibrary對象獲取的其他對象只在該ALAssetsLibrary對象生命期內(nèi)有效, 即若該ALAssetsLibrary對象被銷毀, 則其他從中獲取的對象都不能再訪問了. 因此, 采用以上block的方式會比較方便.
對于ALAssetsLibrary對象,通常采用單例模式。
所有得到的都為CGImageRef對象.
推薦使用fullScreenImage, 因其已調(diào)整過. 而fullResolutionImage尺寸太大, 需要自己調(diào)整和縮放.
PhotoKit使用PHAssetCollection存儲相冊,使用PHAsset存儲每個圖片資源,使用PHImageManager從asset中請求圖片,即關(guān)鍵點(diǎn)在于PHAssetCollection -> PHAsset -> PHImageManager,分別對應(yīng)方法:
fetchAssetCollectionsWithType, fetchAssetsInAssetCollection, requestImageForAsset。而關(guān)于該圖片資源的信息都可以從PHAsset中獲取,如pixelWidth, creationDate, location, favorite等。
PHAssetCollection表示一個相冊或一個時刻,是PHCollection的子類。
PHCollectionList表示一組PHCollection,本身也是一個PHCollection。
PHAsset表示一個資源,跟ALAsset對應(yīng),一個PHAsset可同時屬于多個不同的PHAssetCollection。
PHFetchResult表示所請求資源的數(shù)組,可包含PHAssetCollection,也可包含PHAsset。使用PHFetchOption定義請求資源時的參數(shù)。
PHImageManager通過請求的方式獲取圖片及其參數(shù)。其中requestImageForAsset有多個參數(shù)需注意,這里就不詳細(xì)介紹了。
代碼如下:
- (void)loadPhotos {// 所有智能相冊PHFetchResult *smartAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];for(NSIntegeri=0; i
- (void)loadPhotos {// 所有智能相冊PHFetchResult *smartAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];for(NSIntegeri=0; i
PHAssetCollection *assetCollection = (PHAssetCollection *)collection;// 從一個相冊中獲取的PHFetchResult中包含的才是PHAssetPHFetchResult *fetchResult = [PHAsset fetchAssetsInAssetCollection:assetCollection options:nil];
PHAsset *asset =nil;if(fetchResult.count!=0) {
asset = fetchResult[0];
}// 使用PHImageManager從PHAsset中請求圖片PHImageManager *imageManager = [[PHImageManager alloc] init];
[imageManager requestImageForAsset:asset targetSize:PHImageManagerMaximumSize contentMode:PHImageContentModeAspectFill options:nilresultHandler:^(UIImage* _Nullable result,NSDictionary* _Nullable info) {if(result) {NSLog(@"%@", result);
}
}];
}else{
NSAssert1(NO, @"Fetch collection not PHCollection: %@", collection);
}
}
}
其中,PHFetchResult是一個數(shù)組,可用于存放PHAssetCollection,也可用于存放PHAsset,這一點(diǎn)對于初學(xué)者不易理解。
PhotoKit相對于ALAssetsLibrary的改進(jìn)在于:
1. ALAssetsLibrary獲取數(shù)據(jù),無論相冊還是資源都是枚舉方式(ALAssetLibrary照片庫->ALAssetGroup相冊->ALAsset資源)遍歷照片庫取得相應(yīng)的數(shù)據(jù)。而PhotoKit通過傳入?yún)?shù)直接獲取相應(yīng)的數(shù)據(jù),效率提升不少且靈活,統(tǒng)一用PHFetchResult封裝。
2. 在ALAssetsLibrary中,相冊和資源是對應(yīng)不同的對象(ALAssetGroup和ALAsset),因此獲取接口沒有關(guān)聯(lián)。而PhotoKit中有PHFetchResult這個可以統(tǒng)一儲存相冊或資源的對象,因此更方便。
3. PhotoKit返回資源結(jié)果時,同時返回了資源的元數(shù)據(jù),獲取元數(shù)據(jù)在AssetsLibrary中很難辦到。通過PHAsset,可以直接獲取資源是否被收藏(favorite)和隱藏(hidden),拍攝圖片時是否開啟了HDR或全景模式,甚至能通過一張連拍圖片獲取到連拍圖片中的其他圖片。即PhotoKit能更好與設(shè)備照片庫接入。
PhotoKit的基本使用就介紹到這里,一些高級的用法留待以后進(jìn)一步挖掘。