本文介紹了iOS中獲取系統相冊中的圖片,在很多應用中都能用到,可以獲取單張圖片,也可以同時獲取多張圖片,廢話不多說了,看下面吧。
一.獲取單張圖片
思路:
1.利用UIImagePickerController可以從系統自帶的App(照片\相機)中獲得圖片
2.設置代理,遵守代理協議
注意這個UIImagePickerController類比較特殊,需要遵守兩個代理協議
@interface ViewController () <UIImagePickerControllerDelegate, UINavigationControllerDelegate>
3.實現代理的方法didFinishPickingMediaWithInfo
- (void)getImageFromIpc { // 1.判斷相冊是否可以打開 if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) return; // 2. 創建圖片選擇控制器 UIImagePickerController *ipc = [[UIImagePickerController alloc] init]; /** typedef NS_ENUM(NSInteger, UIImagePickerControllerSourceType) { UIImagePickerControllerSourceTypePhotoLibrary, // 相冊 UIImagePickerControllerSourceTypeCamera, // 用相機拍攝獲取 UIImagePickerControllerSourceTypeSavedPhotosAlbum // 相簿 } */ // 3. 設置打開照片相冊類型(顯示所有相簿) ipc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; // ipc.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum; // 照相機 // ipc.sourceType = UIImagePickerControllerSourceTypeCamera; // 4.設置代理 ipc.delegate = self; // 5.modal出這個控制器 [self presentViewController:ipc animated:YES completion:nil]; } #pragma mark -- <UIImagePickerControllerDelegate>-- // 獲取圖片後的操作 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info { // 銷毀控制器 [picker dismissViewControllerAnimated:YES completion:nil]; // 設置圖片 self.imageView.image = info[UIImagePickerControllerOriginalImage]; }
二.獲取多張圖片
思路:
#import <AssetsLibrary/AssetsLibrary.h>
1.獲得所有相簿的原圖
- (void)getOriginalImages { // 獲得所有的自定義相簿 PHFetchResult<PHAssetCollection *> *assetCollections = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil]; // 遍歷所有的自定義相簿 for (PHAssetCollection *assetCollection in assetCollections) { [self enumerateAssetsInAssetCollection:assetCollection original:YES]; } // 獲得相機膠卷 PHAssetCollection *cameraRoll = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeSmartAlbumUserLibrary options:nil].lastObject; // 遍歷相機膠卷,獲取大圖 [self enumerateAssetsInAssetCollection:cameraRoll original:YES]; }
2.獲得所有相簿中的縮略圖
- (void)getThumbnailImages { // 獲得所有的自定義相簿 PHFetchResult<PHAssetCollection *> *assetCollections = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil]; // 遍歷所有的自定義相簿 for (PHAssetCollection *assetCollection in assetCollections) { [self enumerateAssetsInAssetCollection:assetCollection original:NO]; } // 獲得相機膠卷 PHAssetCollection *cameraRoll = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeSmartAlbumUserLibrary options:nil].lastObject; [self enumerateAssetsInAssetCollection:cameraRoll original:NO]; }
3.遍歷相冊
/** * 遍歷相簿中的所有圖片 * @param assetCollection 相簿 * @param original 是否要原圖 */ - (void)enumerateAssetsInAssetCollection:(PHAssetCollection *)assetCollection original:(BOOL)original { NSLog(@"相簿名:%@", assetCollection.localizedTitle); PHImageRequestOptions *options = [[PHImageRequestOptions alloc] init]; // 同步獲得圖片, 只會返回1張圖片 options.synchronous = YES; // 獲得某個相簿中的所有PHAsset對象 PHFetchResult<PHAsset *> *assets = [PHAsset fetchAssetsInAssetCollection:assetCollection options:nil]; for (PHAsset *asset in assets) { // 是否要原圖 CGSize size = original ? CGSizeMake(asset.pixelWidth, asset.pixelHeight) : CGSizeZero; // 從asset中獲得圖片 [[PHImageManager defaultManager] requestImageForAsset:asset targetSize:size contentMode:PHImageContentModeDefault options:options resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) { NSLog(@"%@", result); }]; } }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持本站。