本文引見了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); }]; } }
以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支持本站。
【iOS中獲取零碎相冊中的圖片實例】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!