iOS 相册多选

使用Photos获取所有图片,在用CollectionView进行排版

1.首先导入#import

2.判断用户是否打开了相册权限

[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {

if (status == PHAuthorizationStatusAuthorized) {

NSLog(@"用户允许授权相册");

[self photoIamge];

}else {

NSLog(@"用户拒绝授权相册");

}

}];

3.获取相册内所有照片资源

- (NSMutableArray*)getAllAssetInPhotoAblumWithAscending:(BOOL)ascending{    
NSMutableArray*assets = [NSMutableArray array];

PHFetchOptions *option = [[PHFetchOptions alloc] init];

//ascending 为YES时,按照照片的创建时间升序排列;为NO时,则降序排列

option.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:ascending]];

PHFetchResult *result = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:option];

[result enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {

PHAsset *asset = (PHAsset *)obj;

//        NSLog(@"照片名%@", [asset valueForKey:@"filename"]);

[assets addObject:asset];

}];

return assets;

}

4.添加图片到CollectionView

-(void)photoIamge

{

phAssets =  [self getAllAssetInPhotoAblumWithAscending:YES];

for (PHAsset *set in phAssets) {

PHImageRequestOptions *options = [[PHImageRequestOptions alloc] init];

options.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat;

options.synchronous=YES;

//原图宽高(不建设使用,太大)

//        CGSize imageSize=CGSizeMake(set.pixelWidth, set.pixelHeight);

[[PHImageManager defaultManager] requestImageForAsset:set targetSize:CGSizeZero contentMode:PHImageContentModeAspectFit options:options resultHandler:^(UIImage *result, NSDictionary *info) {

//设置图片

[array addObject:result];

[self.photoCollectionView reloadData];

}];

}

}

5.点击图片判断是否选择

//单元格个数

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section

{

return array.count;

}

//单元格内容

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath

{

static NSString *string=@"photoCell";

PhotoCollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:string forIndexPath:indexPath];

UIImage *image = array[indexPath.row];

cell.photoImageView.image=image;

cell.selectLabel.text=@"未选择";

return cell;

}

//点击单元格触发事件

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath

{

UIImage *image=[array objectAtIndex:indexPath.row];

PhotoCollectionViewCell *cell=(PhotoCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];

//已经12张图片,如果点击选择的着删除

BOOL select=YES;

//判断是否要删除图片

for (UIImage *selectImage in selectArray) {

if (selectImage==image) {

cell.selectLabel.text=@"未选择";

[selectArray removeObject:image];

select=NO;

break;

}

}

if (select==YES) {

//判断是否超过12张

if (selectArray.count>11) {

//提示不能超过12张

UIAlertController *alert=[UIAlertController alertControllerWithTitle:@"照片不能超过12张" message:nil preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction *confirmAction=[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {

}];

[alert addAction:confirmAction];

[self presentViewController:alert animated:YES completion:^{

}];

}

else

{

if ([cell.selectLabel.text isEqualToString:@"未选择"]) {

cell.selectLabel.text=@"选中";

[selectArray addObject:image];

}

else

{

cell.selectLabel.text=@"未选择";

[selectArray removeObject:image];

}

NSLog(@"%ld",selectArray.count);

NSLog(@"%@",selectArray);

}

}

}

你可能感兴趣的:(iOS 相册多选)