【问题标题】:multiple imagePicker from camera来自相机的多个图像选择器
【发布时间】:2016-12-01 01:47:03
【问题描述】:

我的视图控制器上有 3 个图像视图。我可以选择从画廊中选择或拍照。现在,我想要的是来自相机。我想默认选择图片,我们可以选择一张,但是我想要三个如何实现。

【问题讨论】:

  • 您是否检查了社区中现有的答案? stackoverflow.com/questions/20756899/…
  • UIImagePickerController 不允许您选择多个图像。您可以尝试自定义 ImagePickerController,例如:github.com/B-Sides/ELCImagePickerController,但它可能无法按预期工作,因为该库所基于的类 (ALAssetsLibrary) 自 iOS 9 起已被弃用。
  • we can pick one, but I want three how can we achieve it你能简单解释一下这些行吗

标签: ios objective-c xcode


【解决方案1】:

我向您推荐了两个第三方来选择多个图像并尽可能处理:-

1-ELCImagePickerController

2-CTAssetsPickerController

【讨论】:

  • 两者都不支持设备摄像头图像。
【解决方案2】:

使用TagConcept`

第一步

最初为您的每个 imageView 分配标签为1,2,3 并创建打开UIImagePickerController 的一种常用方法

第二步

为每张图片创建手势

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleImageTap:)];
tap.cancelsTouchesInView = YES;
tap.numberOfTapsRequired = 1;
tap.delegate = self;
 imageView.userInteractionEnabled = YES;
[imageView addGestureRecognizer:tap];

调用方法喜欢

- (IBAction)handleImageTap:(UIGestureRecognizer*)sender

{

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Attach image" message:@"" preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction* pickFromGallery = [UIAlertAction actionWithTitle:@"Take a photo"
                                                          style:UIAlertActionStyleDefault
                                                        handler:^(UIAlertAction * action) {
                                                            if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
                                                            {
                                                                UIImagePickerController* picker = [[UIImagePickerController alloc] init];
                                                                picker.view.tag = sender.view.tag;
                                                                picker.sourceType = UIImagePickerControllerSourceTypeCamera;
                                                                picker.delegate = self;
                                                                picker.allowsEditing = YES;
                                                                [self presentViewController:picker animated:YES completion:NULL];
                                                            }

                                                        }];
UIAlertAction* takeAPicture = [UIAlertAction actionWithTitle:@"Choose from gallery"
                                                       style:UIAlertActionStyleDefault
                                                     handler:^(UIAlertAction * action) {
                                                         UIImagePickerController* picker = [[UIImagePickerController alloc] init];
                                                         picker.view.tag = sender.view.tag;
                                                         picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
                                                         picker.delegate = self;
                                                         picker.allowsEditing = YES;
                                                         [self  presentViewController:picker animated:YES completion:NULL];
                                                     }];
UIAlertAction* cancel = [UIAlertAction actionWithTitle:@"Cancel"
                                                 style:UIAlertActionStyleCancel
                                               handler:^(UIAlertAction * action) {
                                               }];

[alertController addAction:pickFromGallery];
[alertController addAction:takeAPicture];
[alertController addAction:cancel];
[self presentViewController:alertController animated:YES completion:NULL];

}

第三步

在那个委托方法上

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
if(picker.view.tag == 1)
{
 // assign the image to first
}
else if(picker.view.tag == 2)
{
 // assign the image to second
}
else
{

// assign the image to third
}

}

【讨论】:

  • @user3182143 - 为什么我们准备第三方,我们可以在自己的代码中自定义所有内容,非常感谢
  • 对每个人都有用。
  • @Anbu.Karthik :我认为原始帖子要求一次尝试选择三个图像!
  • 是的,OP 要求一次选择 3 个图像,并将它们分别应用于 3 个图像视图。
  • 顺便说一句,提问者应该使用这种方法,因为这更简单。如果有三个图像视图,那么他可以一张一张地挑选三个图像。无需一次选择三张图片!!!
【解决方案3】:

您可以编写自己的逻辑,例如,

didFinishPickingImage 中增加一个计数器并且不要关闭image picker 控制,直到计数器达到 3 并在每次本地存储(如文档目录或临时目录)时存储图像。

请参阅this so post 的已接受答案,以演示我所解释的内容。

如果你想使用第三方库,那么ELCImagePickerController 是更好的解决方案。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多