如何获取手机系统相册中的图片
1> UIImagePickerController
2> AssetsLibrary框架
3> Photos框架
1> AssetsLibrary框架
2> Photos框架
#import "ViewController.h"
#import <Photos/Photos.h>
#import <CTAssetsPickerController/CTAssetsPickerController.h>
@interface ViewController ()<CTAssetsPickerControllerDelegate >
@end
@implementation ViewController
-(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.delegate = self;
//设置相册的类型 : 相机胶卷 + 自定义相册
picker.assetCollectionSubtypes = @[
@(PHAssetCollectionSubtypeSmartAlbumUserLibrary),
@(PHAssetCollectionSubtypeAlbumRegular)
];
//并且不要显示空的相册
picker.showsEmptyAlbums = NO;
//设置所选照片的索引
picker.showsSelectionIndex = YES;
[self presentViewController:picker animated:YES completion:nil];
});
}];
}
#pragma mark - CTAssetsPickerControllerDelegate
//当用户选择照片完成后调用(将图片显示在控制器的View上面)
-(void)assetsPickerController:(CTAssetsPickerController *)picker didFinishPickingAssets:(NSArray *)assets
{
//关闭图片选择界面
[picker dismissViewControllerAnimated:YES completion:nil];
//可以获取所有选中的图片
// NSLog(@"%@",assets);
//获取屏幕像素与点坐标的比值
CGFloat scale = [UIScreen mainScreen].scale;
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 / scale, asset.pixelHeight / scale);
//获取图片
[[PHImageManager defaultManager] requestImageForAsset:asset targetSize:size contentMode:PHImageContentModeDefault options:options resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
//获取图片成功就会来到这个block中
UIImageView *imageView = [[UIImageView alloc] init];
imageView.image = result;
imageView.frame = CGRectMake((i % 3)*92.5, (i / 3)*92.5, 92.5, 92.5);
[self.view addSubview:imageView];
}];
}
}
//控制器是否允许用户去选择照片
-(BOOL)assetsPickerController:(CTAssetsPickerController *)picker shouldSelectAsset:(PHAsset *)asset
{
NSInteger max = 2;
if (picker.selectedAssets.count >= max) {
//提示用户最多只能选择的张数
NSString *massage = [NSString stringWithFormat:@"最多只能选%zd张",max];
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:massage preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:nil];
[alert addAction:action1];
//这里不能使用self来modal别的控制器,因为此时self.view不在window上,如果A想modal出B,那么A必须要在窗口上
//用self会报警告
/*
Warning: Attempt to present <UIAlertController: 0x7fb8f1f3bab0> on <ViewController: 0x7fb8f1c207a0> whose view is not in the window hierarchy!
*/
[picker presentViewController:alert animated:YES completion:nil];
return NO;
}
return YES;
}
@end