photo

作者: Fun箱Dao柜 | 来源:发表于2016-07-04 23:44 被阅读53次
    //
    //  ViewController.m
    //  获取相册图片
    //
    //  Created by xiaomage on 16/4/19.
    //  Copyright © 2016年 小码哥. All rights reserved.
    //
    
    /*
     获取相册中的图片
     1.单张
     1> UIImagePickerController
     2> AssetsLibrary框架
     3> Photos框架
     
     2.多张(>=2张)
     1> AssetsLibrary框架
     2> Photos框架(推荐)
     */
    
    #import "ViewController.h"
    #import <Photos/Photos.h>
    #import <CTAssetsPickerController/CTAssetsPickerController.h>
    
    @interface ViewController () <CTAssetsPickerControllerDelegate>
    @property (nonatomic , strong) NSMutableArray *assets;
    @end
    
    @implementation ViewController
    -(NSMutableArray *)assets{
        if (_assets == nil) {
            _assets = [NSMutableArray array];
        }
        return _assets;
    }
    /*
     // Optionally present picker as a form sheet on iPad
     if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
     picker.modalPresentationStyle = UIModalPresentationFormSheet;
     */
    
    /*
     Attempt to present <UIAlertController: 0x7f893dd34270> on <ViewController: 0x7f893b557830> whose view is not in the window hierarchy
     
     [a modal出: d] : a必须在window上
     */
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        
        // github的搜索关键字:image picker photo asset
    }
    
    - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
        [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status){
            if (status != PHAuthorizationStatusAuthorized) return;
            
            dispatch_async(dispatch_get_main_queue(), ^{
                CTAssetsPickerController *picker = [[CTAssetsPickerController alloc] init];
                picker.selectedAssets = self.assets;
                
                picker.delegate = self;
                // 显示选择的索引
                picker.showsSelectionIndex = YES;
                // 设置相册的类型:相机胶卷 + 自定义相册
                picker.assetCollectionSubtypes = @[
                  @(PHAssetCollectionSubtypeSmartAlbumUserLibrary),
                  @(PHAssetCollectionSubtypeAlbumRegular)];
                // 不需要显示空的相册
                picker.showsEmptyAlbums = NO;
                [self presentViewController:picker animated:YES completion:nil];
            });
        }];
    }
    
    #pragma mark - <CTAssetsPickerControllerDelegate>
    - (BOOL)assetsPickerController:(CTAssetsPickerController *)picker shouldSelectAsset:(PHAsset *)asset
    {
        NSInteger max = 10;
        
        if (picker.selectedAssets.count >= max) {
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:[NSString stringWithFormat:@"最多选择%zd张图片", max] preferredStyle:UIAlertControllerStyleAlert];
            [alert addAction:[UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:nil]];
            [picker presentViewController:alert animated:YES completion:nil];
            // 这里不能使用self来modal别的控制器,因为此时self.view不在window上
            return NO;
        }
        
        return YES;
    }
    
    - (void)assetsPickerController:(CTAssetsPickerController *)picker didFinishPickingAssets:(NSArray *)assets
    {
        // 关闭图片选择界面
        [picker dismissViewControllerAnimated:YES completion:nil];
        
        // 基本配置
        CGFloat scale = [UIScreen mainScreen].scale;
        PHImageRequestOptions *options = [[PHImageRequestOptions alloc] init];
        options.resizeMode   = PHImageRequestOptionsResizeModeExact;
        options.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat;
        self.assets = [NSMutableArray arrayWithArray:assets];
        // 遍历选择的所有图片
        for (NSInteger i = 0; i < assets.count; i++) {
            PHAsset *asset = assets[i];
            CGSize size = CGSizeMake(asset.pixelWidth / scale, asset.pixelHeight / 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;
                imageView.frame = CGRectMake((i % 3) * 110, (i / 3) * 110, 100, 100);
                [self.view addSubview:imageView];
            }];
        }
    }
    @end
    
    

    相关文章

      网友评论

          本文标题:photo

          本文链接:https://www.haomeiwen.com/subject/ivlhjttx.html