如何在iOS中使用swift的多个图像选择器

时间:2016-05-11 15:07:48

标签: ios swift

我正在制作约会应用。 如您所知,用户需要在约会应用中注册多张图片。

所以我得到了如何在一个视图中使用1个图像选择器。

但我不知道如何添加多个图片选择器。

我知道我只能使用一个

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {  
}

func imagePickerControllerDidCancel(picker: UIImagePickerController) {
    picker.dismissViewControllerAnimated(false, completion:nil)
}

所以我无法找到多个imagepicker视图的解决方案。

我的失败代码如下。

import UIKit

class RegisterPicture : UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {


@IBAction func pick1(sender: AnyObject) {

    let picker1 = UIImagePickerController()

    picker1.sourceType = UIImagePickerControllerSourceType.SavedPhotosAlbum

    picker1.allowsEditing = true

    picker1.delegate = self

    self.presentViewController(picker1, animated: false, completion: nil)
}

@IBAction func pick2(sender: AnyObject) {

    let picker2 = UIImagePickerController()

    picker2.sourceType = UIImagePickerControllerSourceType.SavedPhotosAlbum

    picker2.allowsEditing = true

    picker2.delegate = self

    self.presentViewController(picker2, animated: false, completion: nil)
}

@IBOutlet var picture1: UIImageView!

@IBOutlet var picture2: UIImageView!

func imagePickerController(picker1: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {

    picker1.dismissViewControllerAnimated(false, completion : nil)
    self.picture1.image = info[UIImagePickerControllerOriginalImage] as? UIImage

}

func imagePickerController(picker2: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {

    picker2.dismissViewControllerAnimated(false, completion : nil)
    self.picture2.image = info[UIImagePickerControllerOriginalImage] as? UIImage

}


func imagePickerControllerDidCancel(picker1: UIImagePickerController) {

    picker1.dismissViewControllerAnimated(false, completion:nil)

}

func imagePickerControllerDidCancel(picker2: UIImagePickerController) {

    picker2.dismissViewControllerAnimated(false, completion:nil)

}
}

6 个答案:

答案 0 :(得分:3)

您无法使用UIImagePickerController选择多个图像。您要么制作自己的自定义图像选择器,要么使用第三方one

答案 1 :(得分:2)

我试图找到一个类似于iOS照片应用程序的UX的选择器,它针对专辑和优化而优化。资产同步,但我找不到这样的控制器,最后我自己做了。它具有强大的定制能力,支持多种选择,自定义排序和过滤,并在现代Swift 3.0中完全编写。如果有人发现它的错误,我会为你修复它们:) 您可以尝试此链接。 https://github.com/DragonCherry/AssetsPickerViewController

答案 2 :(得分:2)

对于在 2020 年搜索此内容的任何人,Apple 在 iOS14 中引入了 PHPickerViewController。这是文档:https://developer.apple.com/documentation/photokit/phpickerviewcontroller

以及解释其用法的 WWDC 介绍: https://developer.apple.com/videos/play/wwdc2020/10652/

答案 3 :(得分:1)

@IBAction func btnTrophy1Action(_ sender: UIButton) {
        //GIVE TAG TO YOUR BUTTON
        self.view.endEditing(true)
        self.senderTag = sender.tag
        pickerOpen(sender: sender)
    }

@IBAction func btnTrophy2Action(_ sender: UIButton) {
        //GIVE TAG TO YOUR BUTTON
        self.view.endEditing(true)
        self.senderTag = sender.tag
        pickerOpen(sender: sender)
    }

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {

        if let image = info[UIImagePickerController.InfoKey.editedImage] as? UIImage {
            //BASED ON THE TAG SAVE IMAGE TO PARTICULAR IMAGE PICKER
            if self.senderTag == 101 {
                //TROPHY 1
                self.imgTrophy1.image = image
                self.isTrophy1Selected = true
            } else if self.senderTag == 102 {
                //TROPHY 2
                self.imgTrophy2.image = image
                self.isTrophy2Selected = true
            } else {
                //TROPHY 3
                self.imgTrophy3.image = image
                self.isTrophy3Selected = true
            }
        }
        picker.dismiss(animated: true, completion: nil)
    }

答案 4 :(得分:0)

我使用了一个图像选择器,然后使用一个包含字符串的枚举来确定哪个图像视图获取用户选择的图像。

enum selectableImage: String {
        case image1
        case image2
        case image3
    }

然后在每次调用imagePickerController时,像这样分配一个变量var imageSelected = selectableImage.image1。 最后,在imagePickerController中,我像这样使用了交换结构:

 func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {

        print("Inside imageController")

        print(info)
        // The info dictionary may contain multiple representations of the image. You want to use the original.

        guard let imagePicked = info[.originalImage] as? UIImage else {
            fatalError("Expected a dictionary containing an image, but was provided the following: \(info)")
        }

        // Set photoImageView to display the selected image.

        switch imageSelected {

        case selectableImage.image1:
             image1.image = imagePicked

        case selectableImage.image2:
             image2.image = imagePicked

        case selectableImage.image3:
            image3.image = imagePicked

        }


        // Dismiss the picker.
        dismiss(animated: true, completion: nil)
    }

希望这会有所帮助。

答案 5 :(得分:0)

您不能使用UIImagePickerController,但是可以使用自定义图像选择器。我认为ELCImagePickerController是最好的选择,但是这里有一些您可以使用的其他库:

Objective-C

  1. ELCImagePickerController
  2. WSAssetPickerController
  3. QBImagePickerController
  4. ZCImagePickerController
  5. CTAssetsPickerController
  6. AGImagePickerController
  7. UzysAssetsPickerController
  8. MWPhotoBrowser
  9. TSAssetsPickerController
  10. CustomImagePicker
  11. InstagramPhotoPicker
  12. GMImagePicker
  13. DLFPhotosPicker
  14. CombinationPickerController
  15. AssetPicker
  16. BSImagePicker
  17. SNImagePicker
  18. DoImagePickerController
  19. grabKit
  20. IQMediaPickerController
  21. HySideScrollingImagePicker
  22. MultiImageSelector
  23. TTImagePicker
  24. SelectImages
  25. ImageSelectAndSave
  26. imagepicker-multi-select
  27. MultiSelectImagePickerController
  28. YangMingShan(类似于Yahoo的图片选择器)
  29. DBAttachmentPickerController
  30. BRImagePicker
  31. GLAssetGridViewController
  32. CreolePhotoSelection

迅速

  1. LimPicker(类似于WhatsApp的图像选择器)
  2. RMImagePicker
  3. DKImagePickerController
  4. BSImagePicker
  5. Fusuma(Instagram就像图片选择器一样)
  6. YangMingShan(类似于Yahoo的图片选择器)
  7. NohanaImagePicker
  8. ImagePicker
  9. OpalImagePicker
  10. TLPhotoPicker
  11. AssetsPickerViewController
  12. 提示和选择器/电报选择器