標籤:
#import "ViewController.h"#import <Photos/Photos.h>@interface ViewController () <UIImagePickerControllerDelegate, UINavigationControllerDelegate>@property (weak, nonatomic) IBOutlet UIImageView *imageView;@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib.}- (IBAction)selectImage { [self getThumbnailImages];}/** * 獲得所有相簿的原圖 */- (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];}/** * 獲得所有相簿中的縮圖 */- (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];}/** * 遍曆相簿中的所有圖片 * * @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); }]; }}/** * 獲得相機菲林中的所有圖片 */- (void)getImagesFromCameraRoll{ // 獲得相機菲林中的所有圖片 PHFetchResult<PHAsset *> *assets = [PHAsset fetchAssetsWithOptions:nil]; __block int count = 0; for (PHAsset *asset in assets) { [[PHImageManager defaultManager] requestImageForAsset:asset targetSize:CGSizeMake(asset.pixelWidth, asset.pixelHeight) contentMode:PHImageContentModeDefault options:nil resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) { NSLog(@"%@ - %zd", result, count++); }]; }}/** * 利用UIImagePickerController挑選圖片 */- (void)getImageFromIpc{ // UIImagePickerController : 可以從系統內建的App(照片\相機)中獲得圖片 // 判斷相簿是否可以開啟 if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) return; UIImagePickerController *ipc = [[UIImagePickerController alloc] init]; // 開啟照片應用(顯示所有相簿) ipc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; // 開啟照片應用(只顯示"時刻"這個相簿) // ipc.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum; // 照相機 // ipc.sourceType = UIImagePickerControllerSourceTypeCamera; ipc.delegate = self; [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];}@end
總結:1:從相簿中選取照片可以利用UIImagePickerController,前提是必須遵守兩個協議:
<UIImagePickerControllerDelegate, UINavigationControllerDelegate>但是此種方法只能擷取到相簿中的一張照片。使用方法如下:1:可以先判斷其是否支援相機或是相簿,不支援直接return,不執行以下代碼 2:建立對象,設定代理,在設定sourceType(注意兩種類型的區別),彈出就用presentViewController可以彈出相簿,dissmiss返回到應用。實現代理方法擷取圖片: self.imageView.image = info[UIImagePickerControllerOriginalImage];3:UIImagePickerController顯示中文介面:
1.Project-->Info-->Localizations添加Chinese
2.修改Target-->Info-->Localization native development region : China
- (void)getImageFromIpc
{
// UIImagePickerController : 可以從系統內建的App(照片\相機)中獲得圖片
// 判斷相簿是否可以開啟
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) return;
UIImagePickerController *ipc = [[UIImagePickerController alloc] init];
// 開啟照片應用(顯示所有相簿)
ipc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
// 開啟照片應用(只顯示"時刻"這個相簿)
// ipc.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
// 照相機
// ipc.sourceType = UIImagePickerControllerSourceTypeCamera;
ipc.delegate = self;
[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];
}
2:若是想多選照片:1:可用系統架構:#import <Photos/Photos.h> 2:使用步驟:1:先獲得所有相簿,在獲得相簿菲林
PHAssetCollection
2:遍曆相簿數組,得到每一個相簿照片PHAsset,再利用PHAsset請求獲得每一張照片
3:若想做成系統相簿的樣式:1:此介面可採用九宮格搭建,或是採用collection搭建,建議用後者,因為後者可以有迴圈利用的機制,節省
記憶體
2:如下介面搭建:可以監聽每張圖片的點擊,當點擊到某張圖片的時候,將封裝好的蒙板和對勾,蓋在上面,或是再移除
ios開發多選照片實現