一.獲取單張圖片
思路:
- 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.判斷相冊是否可以打開
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) return;
// 2. 創(chuàng)建圖片選擇控制器
UIImagePickerController *ipc = [[UIImagePickerController alloc] init];
/**
typedef NS_ENUM(NSInteger, UIImagePickerControllerSourceType) {
UIImagePickerControllerSourceTypePhotoLibrary, // 相冊
UIImagePickerControllerSourceTypeCamera, // 用相機(jī)拍攝獲取
UIImagePickerControllerSourceTypeSavedPhotosAlbum // 相簿
}
*/
// 3. 設(shè)置打開照片相冊類型(顯示所有相簿)
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ā)送請求才能從asset獲取圖片
- PHImageRequestOptions圖片請求選項(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.遍歷相冊
/**
* 遍歷相簿中的所有圖片
* @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);
}];
}
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者