美文网首页iOS常用
ios 选择系统相册多张图片功能

ios 选择系统相册多张图片功能

作者: 肉肉要次肉 | 来源:发表于2020-09-07 15:57 被阅读0次

CTAssetsPickerController通过它来实现

pod 'CTAssetsPickerController',  '~> 3.3.0'

导入文件之后,在你需要用到的页面记得引入头文件

#import <Photos/PHPhotoLibrary.h>

#import<CTAssetsPickerController/CTAssetsPickerController.h>

签协议CTAssetsPickerControllerDelegate

-(void)saveAction{

    [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {

        if (status != PHAuthorizationStatusAuthorized) return ;

        dispatch_async(dispatch_get_main_queue(), ^{

        //弹出控制器

            CTAssetsPickerController *assetPC = [[CTAssetsPickerController alloc] init];

            //隐藏空相册

            assetPC.showsEmptyAlbums=YES;

            //显示图片索引

            assetPC.showsSelectionIndex=YES;

            //显示那些资源

            assetPC.assetCollectionSubtypes = @[@(PHAssetCollectionSubtypeSmartAlbumUserLibrary), @(PHAssetCollectionSubtypeAlbumRegular)];

            assetPC.delegate=self;

            if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) //如果是运行到ipad上面

            {

                assetPC.modalPresentationStyle = UIModalPresentationFormSheet;

            }

            [self presentViewController:assetPC animated:YES completion:nil];

        });

    }];

}

#pragma mark - CTAssetsPickerControllerDelegate -

// 限定选择的最大选择个数

- (BOOL)assetsPickerController:(CTAssetsPickerController *)picker shouldSelectAsset:(PHAsset *)asset{

    NSIntegermaxNumber =9;

    if(picker.selectedAssets.countreturnYES;

    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:[NSString stringWithFormat:@"最多选择%ld张", (long)maxNumber] preferredStyle:UIAlertControllerStyleAlert];

    [alertaddAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil]];

     [pickerpresentViewController:alert animated:YES completion:nil];

    return NO;

}

- (void)assetsPickerController:(CTAssetsPickerController *)picker didFinishPickingAssets:(NSArray *)assets{

    //选择完毕关闭页面

    [pickerdismissViewControllerAnimated:YES completion:nil];

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

    option.resizeMode=  PHImageRequestOptionsResizeModeExact;

    option.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat;

    //生成图片

    for(inti =0; i < assets.count; i++){

        NSLog(@"%@",[NSThread currentThread]);

        PHAsset*asset = assets[i];

        CGSize asseSize = CGSizeMake(asset.pixelWidth / [UIScreen mainScreen].scale, asset.pixelHeight / [UIScreen mainScreen].scale);

        [[PHImageManager defaultManager] requestImageForAsset:asset targetSize:asseSize contentMode:PHImageContentModeDefault options:option resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {

这里就拿到了你选择的多张图片,接下来的操作就是看你自己是显示还是需要做什么进一步的操作了。

//显示出你选择的多张图片

//            UIImageView *imageView = [[UIImageView alloc] init];

//

//            imageView.image = result;

//            [self.view addSubview:imageView];

//            imageView.frame = CGRectMake((i %3) *(100 + 10), (i / 3 * (100 + 10)), 100, 100);

//分享到微信好友、朋友圈、收藏,这三个场景是不支持多张图片的,用微信sdk来实现的话,只能是一张

//            WXImageObject *imgObj = [WXImageObject object];

//            NSData *imagedata = UIImagePNGRepresentation(result);

//            imgObj.imageData = imagedata;

//

//            WXMediaMessage *message =[WXMediaMessage message];

//            message.mediaObject = imgObj;

//            SendMessageToWXReq* req = [[SendMessageToWXReq alloc] init];

//            req.bText = NO;

//            req.message = message;

//            req.scene = WXSceneFavorite;

//            [WXApi sendReq:req completion:nil];

        }];

    }

}

当你选好几张图片之后,选择分享到朋友圈时,默认就是选择一张

相关文章

网友评论

    本文标题:ios 选择系统相册多张图片功能

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