photo

作者: 吃土豆不吐葡萄皮 | 来源:发表于2016-05-07 17:05 被阅读40次
    #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.
        
    //    label.text =  NSLocalizedStringFromTable(@"Name", @"Test", nil);
    }
    
    - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
        [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status){
            dispatch_async(dispatch_get_main_queue(), ^{
                
                CTAssetsPickerController *picker = [[CTAssetsPickerController alloc] init];
                
                picker.delegate = self;
                
                picker.assetCollectionSubtypes =
                @[@(PHAssetCollectionSubtypeSmartAlbumUserLibrary),
                  @(PHAssetCollectionSubtypeAlbumRegular)];
                
                [self presentViewController:picker animated:YES completion:nil];
                
            });
        }];
    }
    
    #pragma mark - <CTAssetsPickerControllerDelegate>
    - (void)assetsPickerController:(CTAssetsPickerController *)picker didFinishPickingAssets:(NSArray *)assets
    {
        // 关闭图片选择界面
        [picker dismissViewControllerAnimated:YES completion:nil];
        
        // 取出所有选择的图片
        PHImageRequestOptions *requestOptions = [[PHImageRequestOptions alloc] init];
        requestOptions.resizeMode   = PHImageRequestOptionsResizeModeExact;
        requestOptions.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat;
        PHImageManager *manager = [PHImageManager defaultManager];
        NSInteger index = 0;
        for (PHAsset *asset in assets) {
            [manager requestImageForAsset:asset
                               targetSize:CGSizeMake(100, 100)
                              contentMode:PHImageContentModeAspectFill
                                  options:requestOptions
                            resultHandler:^(UIImage *image, NSDictionary *info){
                                UIImageView *imageView = [[UIImageView alloc] init];
                                imageView.image = image;
                                imageView.frame = CGRectMake(110 * (index % 3), 110 * (index / 3), 100, 100);
                                [self.view addSubview:imageView];
                            }];
            
            index++;
        }
    }
    
    @end
    
    
    
    

    相关文章

      网友评论

          本文标题:photo

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