如何在不調(diào)用ImagePickerController的情況下獲取手機(jī)相冊(cè)中圖片信息

#pragma mark -- iOS 8.0 以上獲取所有照片用Photos.h這個(gè)庫(kù)

-(NSMutableArray *)getALLphotosUsingPohotKit

{

? ? NSMutableArray *arr = [NSMutableArray array];

? ? // 所有智能相冊(cè)

? ? PHFetchResult *smartAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];

? ? for (NSInteger i = 0; i < smartAlbums.count; i++) {

? ? ? ? @autoreleasepool {

? ? ? ? ? ? // 是否按創(chuàng)建時(shí)間排序

? ? ? ? ? ? PHFetchOptions *option = [[PHFetchOptions alloc] init];

? ? ? ? ? ? option.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES]];

? ? ? ? ? ? option.predicate = [NSPredicate predicateWithFormat:@"mediaType == %ld", PHAssetMediaTypeImage];

? ? ? ? ? ? PHCollection *collection = smartAlbums[i];

? ? ? ? ? ? //遍歷獲取相冊(cè)

? ? ? ? ? ? if ([collection isKindOfClass:[PHAssetCollection class]]) {

? ? ? ? ? ? ? ? if ([collection.localizedTitle isEqualToString:@"所有照片"]) {

? ? ? ? ? ? ? ? ? ? PHAssetCollection *assetCollection = (PHAssetCollection *)collection;

? ? ? ? ? ? ? ? ? ? PHFetchResult *fetchResult = [PHAsset fetchAssetsInAssetCollection:assetCollection options:nil];

? ? ? ? ? ? ? ? ? ? NSArray *assets;

? ? ? ? ? ? ? ? ? ? if (fetchResult.count > 0) {

? ? ? ? ? ? ? ? ? ? ? ? // 某個(gè)相冊(cè)里面的所有PHAsset對(duì)象

? ? ? ? ? ? ? ? ? ? ? ? assets = [self getAllPhotosAssetInAblumCollection:assetCollection ascending:YES ];

? ? ? ? ? ? ? ? ? ? ? ? [arr addObjectsFromArray:assets];

? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? }

? ? ? ? ? ? }

? ? ? ? }

? ? }

? ? //返回所有照片內(nèi)的所有照片

? ? return arr;

}

#pragma mark - <? 獲取相冊(cè)里的所有圖片的PHAsset對(duì)象? >

- (NSArray *)getAllPhotosAssetInAblumCollection:(PHAssetCollection *)assetCollection ascending:(BOOL)ascending

{

? ? // 存放所有圖片對(duì)象

? ? NSMutableArray *assets = [NSMutableArray array];

? ? // 是否按創(chuàng)建時(shí)間排序

? ? PHFetchOptions *option = [[PHFetchOptions alloc] init];

? ? option.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:ascending]];

? ? option.predicate = [NSPredicate predicateWithFormat:@"mediaType == %ld", PHAssetMediaTypeImage];


? ? // 獲取所有圖片對(duì)象

? ? PHFetchResult *result = [PHAsset fetchAssetsInAssetCollection:assetCollection options:option];

? ? // 遍歷

? ? [result enumerateObjectsUsingBlock:^(PHAsset *asset, NSUInteger idx, BOOL * _Nonnull stop) {

? ? ? ? [assets addObject:asset];

? ? }];

? ? return assets;

}

調(diào)用方法:

其中,filename就是圖片名字;格式:IMG_0776.JPG

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容