#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
网友评论