iOS中獲取系統(tǒng)相冊(cè)中的圖片實(shí)例
本文介紹了iOS中獲取系統(tǒng)相冊(cè)中的圖片,在很多應(yīng)用中都能用到,可以獲取單張圖片,也可以同時(shí)獲取多張圖片,廢話不多說了,看下面吧。
一.獲取單張圖片
思路:
1.利用UIImagePickerController可以從系統(tǒng)自帶的App(照片\相機(jī))中獲得圖片
2.設(shè)置代理,遵守代理協(xié)議
注意這個(gè)UIImagePickerController類比較特殊,需要遵守兩個(gè)代理協(xié)議
@interface ViewController () <UIImagePickerControllerDelegate, UINavigationControllerDelegate>
3.實(shí)現(xiàn)代理的方法didFinishPickingMediaWithInfo
- (void)getImageFromIpc { // 1.判斷相冊(cè)是否可以打開 if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) return; // 2. 創(chuàng)建圖片選擇控制器 UIImagePickerController *ipc = [[UIImagePickerController alloc] init]; /** typedef NS_ENUM(NSInteger, UIImagePickerControllerSourceType) { UIImagePickerControllerSourceTypePhotoLibrary, // 相冊(cè) UIImagePickerControllerSourceTypeCamera, // 用相機(jī)拍攝獲取 UIImagePickerControllerSourceTypeSavedPhotosAlbum // 相簿 } */ // 3. 設(shè)置打開照片相冊(cè)類型(顯示所有相簿) ipc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; // ipc.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum; // 照相機(jī) // ipc.sourceType = UIImagePickerControllerSourceTypeCamera; // 4.設(shè)置代理 ipc.delegate = self; // 5.modal出這個(gè)控制器 [self presentViewController:ipc animated:YES completion:nil]; } #pragma mark -- <UIImagePickerControllerDelegate>-- // 獲取圖片后的操作 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info { // 銷毀控制器 [picker dismissViewControllerAnimated:YES completion:nil]; // 設(shè)置圖片 self.imageView.image = info[UIImagePickerControllerOriginalImage]; }
二.獲取多張圖片
思路:
- 導(dǎo)入頭文件#import <Photos/Photos.h>
- PHAsset : 一個(gè)資源, 比如一張圖片\一段視頻
- PHAssetCollection : 一個(gè)相簿
- PHImageManager 圖片管理者,是單例,發(fā)送請(qǐng)求才能從asset獲取圖片
- PHImageRequestOptions圖片請(qǐng)求選項(xiàng)
- 注意:這個(gè)類是iOS8開始推廣,iOS9開始廢棄之前的方法
- 系統(tǒng)適配iOS8之前,用下面這個(gè)庫里面的API
#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]; } // 獲得相機(jī)膠卷 PHAssetCollection *cameraRoll = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeSmartAlbumUserLibrary options:nil].lastObject; // 遍歷相機(jī)膠卷,獲取大圖 [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]; } // 獲得相機(jī)膠卷 PHAssetCollection *cameraRoll = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeSmartAlbumUserLibrary options:nil].lastObject; [self enumerateAssetsInAssetCollection:cameraRoll original:NO]; }
3.遍歷相冊(cè)
/** * 遍歷相簿中的所有圖片 * @param assetCollection 相簿 * @param original 是否要原圖 */ - (void)enumerateAssetsInAssetCollection:(PHAssetCollection *)assetCollection original:(BOOL)original { NSLog(@"相簿名:%@", assetCollection.localizedTitle); PHImageRequestOptions *options = [[PHImageRequestOptions alloc] init]; // 同步獲得圖片, 只會(huì)返回1張圖片 options.synchronous = YES; // 獲得某個(gè)相簿中的所有PHAsset對(duì)象 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); }]; } }
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
IOS開發(fā) 支持https請(qǐng)求以及ssl證書配置詳解
這篇文章主要介紹了IOS開發(fā) 支持https請(qǐng)求以及ssl證書配置詳解的相關(guān)資料,需要的朋友可以參考下2017-02-02使用iOS控件UICollectionView生成可拖動(dòng)的桌面的實(shí)例
本篇文章主要介紹了使用iOS控件UICollectionView生成可拖動(dòng)的桌面,具有一定的參考價(jià)值,有需要的可以了解一下。2016-12-12實(shí)例講解如何在iOS應(yīng)用開發(fā)中使用設(shè)計(jì)模式中的代理模式
這篇文章主要介紹了實(shí)例講解如何在iOS應(yīng)用開發(fā)中使用設(shè)計(jì)模式中的代理模式,示例為傳統(tǒng)的Objective-C語言代碼,需要的朋友可以參考下2016-03-03