相信大家對單張圖片的選擇都比較熟悉,簡單的UIImagePickerController就實現了
但是,這裏多張圖片實現就相對比較麻煩,先簡單的展示一下我們最終要實現的效果
1) ALAssetsLibrary類: ALAssetsLibrary類可以實現查看相冊列表,增加相冊,保存圖片到相冊等功能。 例如enumerateGroupsWithTypes方法列舉所有相冊。 (2) ALAssetsGroup *ALAssetsGroup就是相冊的類,可以通過valueForProperty方法查看不同屬性的值,如:ALAssetsGroupPropertyName,相冊名。 *ALAssetsGroup類有幾個方法,posterImage方法就是相冊的封面圖片,numberOfAssets方法獲取該相冊的圖片視頻數量,可以通過enumerateAssetsUsingBlock方法列舉出所有照片。 *ALAssetsGroup 可以使用setAssetsFilter:(ALAssetsFilter *)filter過濾照片或者視頻等。 首先是獲取所有相冊,通過ALAssetsLibrary的實例方法得到ALAssetsGroup類數組。
ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc] init]; NSMutableArray *groupArray = [NSMutableArray array]; [assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) { if (group) { [groupArray addObject:group]; //通過這個可以知道相冊的名字,從而也可以知道安裝的部分應用 NSLog(@"%@",group); } } failureBlock:^(NSError *error) { NSLog(@"Group not found!\n"); }];
(3) ALAsset
*ALAsset類也可以通過valueForProperty方法查看不同屬性的值,如:ALAssetPropertyType,asset的類型,有三種ALAssetTypePhoto, ALAssetTypeVideo or ALAssetTypeUnknown。
*另外還可以通過該方法獲取ALAssetPropertyLocation(照片位置),ALAssetPropertyDuration(視頻時間),ALAssetPropertyDate(照片拍攝日期)等。
可以通過thumbnail方法就是獲取該照片。
*根據相冊獲取該相冊下所有圖片,通過ALAssetsGroup的實例方法得到ALAsset類數組。
[_group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) { if (result) { [imageArray addObject:result]; NSLog(@"%@",result); iv.image=[UIImage imageWithCGImage: result.thumbnail]; NSString *type=[result valueForProperty:ALAssetPropertyType]; } }];
(4) ALAssetsFilter:提供過濾照片或者視頻的實例等。
(5) ALAssetRepresentation
ALAsset類有一個defaultRepresentation方法,返回值是ALAssetRepresentation類,該類的作用就是獲取該資源圖片的詳細資源信息。
// //獲取資源圖片的詳細資源信息 ALAssetRepresentation* representation = [asset defaultRepresentation]; //獲取資源圖片的長寬 CGSize dimension = [representation dimensions]; //獲取資源圖片的高清圖 [representation fullResolutionImage]; //獲取資源圖片的全屏圖 [representation fullScreenImage]; //獲取資源圖片的名字 NSString* filename = [representation filename]; NSLog(@"filename:%@",filename); //縮放倍數 [representation scale]; //圖片資源容量大小 [representation size]; //圖片資源原數據 [representation metadata]; //旋轉方向 [representation orientation]; //資源圖片url地址,該地址和ALAsset通過ALAssetPropertyAssetURL獲取的url地址是一樣的 NSURL* url = [representation url]; NSLog(@"url:%@",url); //資源圖片uti,唯一標示符 NSLog(@"uti:%@",[representation UTI]);
photoKit獲取所有照片
- (NSMutableArray *)getAllPhoto{ NSMutableArray *arr = [NSMutableArray array]; // 所有智能相冊 PHFetchResult *smartAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil]; for (NSInteger i = 0; i < smartAlbums.count; i++) { PHCollection *collection = smartAlbums[i]; //遍歷獲取相冊 if ([collection isKindOfClass:[PHAssetCollection class]]) { PHAssetCollection *assetCollection = (PHAssetCollection *)collection; PHFetchResult *fetchResult = [PHAsset fetchAssetsInAssetCollection:assetCollection options:nil]; PHAsset *asset = nil; if (fetchResult.count != 0) { for (NSInteger j = 0; j < fetchResult.count; j++) { //從相冊中取出照片 asset = fetchResult[j]; PHImageRequestOptions *opt = [[PHImageRequestOptions alloc]init]; opt.synchronous = YES; PHImageManager *imageManager = [[PHImageManager alloc] init]; [imageManager requestImageForAsset:asset targetSize:PHImageManagerMaximumSize contentMode:PHImageContentModeAspectFill options:opt resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) { if (result) { [arr addObject:result]; } }]; } } } } //返回所有照片 return arr;
我是用AssetsLibrary實現的,這裏就簡單介紹一下實現的步驟,首先添加照片,肯定是用CollectionView來實現的,聲明一個數組存放選中的照片,當個照片數量爲0是,返回一個cell,添加的點擊時間訪問圖庫,否則,返回cell的個數爲照片數量加1,設置cell的樣式,當cell的index小於等於照片數量,點擊可以放大圖片,點擊紅色按鈕可以刪除照片,否則的話添加照片,並且改變collectionView的frame