相册的选择-单张/多张

Posted stevenhusir

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了相册的选择-单张/多张相关的知识,希望对你有一定的参考价值。

一、相册的选择-单张(系统-打开相册和打开相机??)

/*
 一、从相册里面选择图片到App中
 1.选择单张图片
 1> UIImagePickerController (自带选择界面)
 2> AssetsLibrary框架 (选择界面需要开发者自己搭建)
 3> Photos框架 (选择界面需要开发者自己搭建)
 
 2.选择多张图片(图片数量 >= 2)
 1> AssetsLibrary框架 (选择界面需要开发者自己搭建)
 2> Photos框架 (选择界面需要开发者自己搭建)
 
 二、利用照相机拍一张照片到App
 1> UIImagePickerController (自带选择界面)
 2> AVCapture****,比如AVCaptureSeession
 */

#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>

@interface ViewController () <UINavigationControllerDelegate, UIImagePickerControllerDelegate>
@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)selectPhoto {
    // UIAlertControllerStyleAlert : 相当于弹出一个UIAlertView
    // UIAlertControllerStyleActionSheet : 相当于弹出一个UIActionSheet
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"请选择方式" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    // 按钮
    [alert addAction:[UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self openAlbum];
    }]];
    
    [alert addAction:[UIAlertAction actionWithTitle:@"照相机" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self openCamera];
    }]];
    
    [self presentViewController:alert animated:YES completion:nil];
}

/**
 *  打开相册
 */
- (void)openAlbum
{
    // UIImagePickerControllerSourceTypeSavedPhotosAlbum : 从Moments相册中选一张图片
    // UIImagePickerControllerSourceTypePhotoLibrary : 从所有相册中选一张图片
    // UIImagePickerControllerSourceTypeCamera : 利用照相机中拍一张图片
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    picker.delegate = self;
    [self presentViewController:picker animated:YES completion:nil];
}

/**
 *  打开照相机
 */
- (void)openCamera
{
    //判断是否能打开相机
    if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) return;
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;
    picker.delegate = self;
    [self presentViewController:picker 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

二、相册的选择-单张/多张(第三方CTAssetsPickerController)

//platform :ios, ‘8.0‘
//pod ‘CTAssetsPickerController‘,  ‘~> 3.3.0‘
/*
 从相册中选择多张图片(图片数量 >= 2)
 1> AssetsLibrary框架 (选择界面需要开发者自己搭建)
 2> Photos框架 (选择界面需要开发者自己搭建)
 */

#import "ViewController.h"
#import <CTAssetsPickerController.h>

@interface ViewController () <CTAssetsPickerControllerDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    // photo  image picker asset
}

- (IBAction)selectPhoto {
    [phphotoLibrary requestAuthorization:^(PHAuthorizationStatus status){
        if (status != PHAuthorizationStatusAuthorized) return;
        
        dispatch_async(dispatch_get_main_queue(), ^{
            // 弹出图片选择界面
            CTAssetsPickerController *picker = [[CTAssetsPickerController alloc] init];
            // 隐藏空相册
            picker.showsEmptyAlbums = NO;
            // 显示图片索引
            picker.showsSelectionIndex = YES;
            picker.assetCollectionSubtypes = @[
                                               @(PHAssetCollectionSubtypeSmartAlbumUserLibrary),
                                               @(PHAssetCollectionSubtypeAlbumRegular)
                                               ];
            picker.delegate = self;
            
            if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { // 如果当前运行的设备是平板电脑
                picker.modalPresentationStyle = UIModalPresentationFormSheet;
            }
            
            [self presentViewController:picker animated:YES completion:nil];
        });
    }];
}

#pragma mark - <CTAssetsPickerControllerDelegate>
- (BOOL)assetsPickerController:(CTAssetsPickerController *)picker shouldSelectAsset:(PHAsset *)asset
{
    NSInteger max = 9;
    
    if (picker.selectedAssets.count < max) return YES;
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"注意"
                                        message:[NSString stringWithFormat:@"最多选择%zd张图片", max]
                                 preferredStyle:UIAlertControllerStyleAlert];
    [alert addAction:[UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:nil]];
    // 这里一定要用picker,不能使用self(因为当前显示在上面的控制器是picker,不是self)
    [picker presentViewController:alert animated:YES completion:nil];
    
    return NO;
}

/**
 *  选择完毕的时候调用
 */
- (void)assetsPickerController:(CTAssetsPickerController *)picker didFinishPickingAssets:(NSArray *)assets
{
    // 关闭图片选择界面
    [picker dismissViewControllerAnimated:YES completion:nil];
    
    // 选择图片时的配置项
    PHImageRequestOptions *options = [[PHImageRequestOptions alloc] init];
    options.resizeMode   = PHImageRequestOptionsResizeModeExact;
    options.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat;
    
    // 显示图片
    for (NSInteger i = 0; i < assets.count; i++) {
        PHAsset *asset = assets[i];
        CGSize size = CGSizeMake(asset.pixelWidth / [UIScreen mainScreen].scale, asset.pixelHeight / [UIScreen mainScreen].scale);
        
        // 请求图片
        [[PHImageManager defaultManager] requestImageForAsset:asset targetSize:size contentMode:PHImageContentModeDefault options:options resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
            // 添加图片控件
            UIImageView *imageView = [[UIImageView alloc] init];
            imageView.image = result;
            [self.view addSubview:imageView];
            
            imageView.frame = CGRectMake((i % 3) * (100 + 10), (i / 3) * (100 + 10), 100, 100);
        }];
    }
}
@end

三、本地化

给项目添加语言支持-选择简体中文 

技术分享图片

 

 国际化-本地化

 

技术分享图片

 

 

 

 

 

以上是关于相册的选择-单张/多张的主要内容,如果未能解决你的问题,请参考以下文章

iOS相册相机(单张图片选取)

idea运行项目时单张图片显示多张图片就不显示了

pyqt5实现按键后切换显示图片,我会显示单张图像但不知道,多张图像怎么搞?

Android仿微信多图片选择,拍照等,适合快速导入到自己项目中

iOS相册(多张图片选取)

iOS之保存图片到系统相册和从系统相册选取一张或者多张照片