本文介紹了iOS中獲取系統(tǒng)相冊(cè)中的圖片,在很多應(yīng)用中都能用到,可以獲取單張圖片,也可以同時(shí)獲取多張圖片,廢話不多說(shuō)了,看下面吧。
一.獲取單張圖片
思路:
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è)是否可以打開(kāi) 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è)置打開(kāi)照片相冊(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];}
二.獲取多張圖片
思路:
#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); }]; }}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。
新聞熱點(diǎn)
疑難解答
圖片精選