iOS中获取系统相册中的图片实例
本文介绍了iOS中获取系统相册中的图片,在很多应用中都能用到,可以获取单张图片,也可以同时获取多张图片,废话不多说了,看下面吧。
一.获取单张图片
思路:
1.利用UIImagePickerController可以从系统自带的App(照片\相机)中获得图片
2.设置代理,遵守代理协议
注意这个UIImagePickerController类比较特殊,需要遵守两个代理协议
@interfaceViewController()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
3.实现代理的方法didFinishPickingMediaWithInfo
-(void)getImageFromIpc
{
//1.判断相册是否可以打开
if(![UIImagePickerControllerisSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])return;
//2.创建图片选择控制器
UIImagePickerController*ipc=[[UIImagePickerControlleralloc]init];
/**
typedefNS_ENUM(NSInteger,UIImagePickerControllerSourceType){
UIImagePickerControllerSourceTypePhotoLibrary,//相册
UIImagePickerControllerSourceTypeCamera,//用相机拍摄获取
UIImagePickerControllerSourceTypeSavedPhotosAlbum//相簿
}
*/
//3.设置打开照片相册类型(显示所有相簿)
ipc.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
//ipc.sourceType=UIImagePickerControllerSourceTypeSavedPhotosAlbum;
//照相机
//ipc.sourceType=UIImagePickerControllerSourceTypeCamera;
//4.设置代理
ipc.delegate=self;
//5.modal出这个控制器
[selfpresentViewController:ipcanimated:YEScompletion:nil];
}
#pragmamark--<UIImagePickerControllerDelegate>--
//获取图片后的操作
-(void)imagePickerController:(UIImagePickerController*)pickerdidFinishPickingMediaWithInfo:(NSDictionary<NSString*,id>*)info
{
//销毁控制器
[pickerdismissViewControllerAnimated:YEScompletion:nil];
//设置图片
self.imageView.image=info[UIImagePickerControllerOriginalImage];
}
二.获取多张图片
思路:
- 导入头文件#import<Photos/Photos.h>
- PHAsset:一个资源,比如一张图片\一段视频
- PHAssetCollection:一个相簿
- PHImageManager图片管理者,是单例,发送请求才能从asset获取图片
- PHImageRequestOptions图片请求选项
- 注意:这个类是iOS8开始推广,iOS9开始废弃之前的方法
- 系统适配iOS8之前,用下面这个库里面的API
#import<AssetsLibrary/AssetsLibrary.h>
1.获得所有相簿的原图
-(void)getOriginalImages
{
//获得所有的自定义相簿
PHFetchResult<PHAssetCollection*>*assetCollections=[PHAssetCollectionfetchAssetCollectionsWithType:PHAssetCollectionTypeAlbumsubtype:PHAssetCollectionSubtypeAlbumRegularoptions:nil];
//遍历所有的自定义相簿
for(PHAssetCollection*assetCollectioninassetCollections){
[selfenumerateAssetsInAssetCollection:assetCollectionoriginal:YES];
}
//获得相机胶卷
PHAssetCollection*cameraRoll=[PHAssetCollectionfetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbumsubtype:PHAssetCollectionSubtypeSmartAlbumUserLibraryoptions:nil].lastObject;
//遍历相机胶卷,获取大图
[selfenumerateAssetsInAssetCollection:cameraRolloriginal:YES];
}
2.获得所有相簿中的缩略图
-(void)getThumbnailImages
{
//获得所有的自定义相簿
PHFetchResult<PHAssetCollection*>*assetCollections=[PHAssetCollectionfetchAssetCollectionsWithType:PHAssetCollectionTypeAlbumsubtype:PHAssetCollectionSubtypeAlbumRegularoptions:nil];
//遍历所有的自定义相簿
for(PHAssetCollection*assetCollectioninassetCollections){
[selfenumerateAssetsInAssetCollection:assetCollectionoriginal:NO];
}
//获得相机胶卷
PHAssetCollection*cameraRoll=[PHAssetCollectionfetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbumsubtype:PHAssetCollectionSubtypeSmartAlbumUserLibraryoptions:nil].lastObject;
[selfenumerateAssetsInAssetCollection:cameraRolloriginal:NO];
}
3.遍历相册
/**
*遍历相簿中的所有图片
*@paramassetCollection相簿
*@paramoriginal是否要原图
*/
-(void)enumerateAssetsInAssetCollection:(PHAssetCollection*)assetCollectionoriginal:(BOOL)original
{
NSLog(@"相簿名:%@",assetCollection.localizedTitle);
PHImageRequestOptions*options=[[PHImageRequestOptionsalloc]init];
//同步获得图片,只会返回1张图片
options.synchronous=YES;
//获得某个相簿中的所有PHAsset对象
PHFetchResult<PHAsset*>*assets=[PHAssetfetchAssetsInAssetCollection:assetCollectionoptions:nil];
for(PHAsset*assetinassets){
//是否要原图
CGSizesize=original?CGSizeMake(asset.pixelWidth,asset.pixelHeight):CGSizeZero;
//从asset中获得图片
[[PHImageManagerdefaultManager]requestImageForAsset:assettargetSize:sizecontentMode:PHImageContentModeDefaultoptions:optionsresultHandler:^(UIImage*_Nullableresult,NSDictionary*_Nullableinfo){
NSLog(@"%@",result);
}];
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。