从库中的视图中挑选几张图片 swift

pick several image in a view from library swift

我开始 swift 我想从 iPhone 库中挑选一些图像到一些 UIImageViews。 我使用了下面的代码,但我的问题是每次我选择一个新图像时,它都放在以前的 UIImageView 上,而不是在它自己的 UIImageview 上。

let imagePicker = UIImagePickerController()
@IBOutlet var img1: UIImageView!
@IBOutlet var img2: UIImageView!
 @IBAction func addphotofct(sender: UIButton!){



    if (img1.hidden == true){


        imagePicker.allowsEditing = false
        imagePicker.sourceType = .PhotoLibrary

        presentViewController(imagePicker, animated: true, completion: nil)
        img1.hidden = false
        img1.userInteractionEnabled = true



    }else  (img2.hidden == true) {


        imagePicker.allowsEditing = false
        imagePicker.sourceType = .PhotoLibrary

        presentViewController(imagePicker, animated: true, completion: nil)
        img2.hidden = false
        img2.userInteractionEnabled = true

}


override func viewDidLoad() {
    super.viewDidLoad()
imagePicker.delegate = self
 }

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
    if let pickedImage1 = info[UIImagePickerControllerOriginalImage] as? UIImage {
        img1.contentMode = .ScaleAspectFit
        img1.image = pickedImage1
    }

    dismissViewControllerAnimated(true, completion: nil)
    }
    func imagePickerController2(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
    if let pickedImage2 = info[UIImagePickerControllerOriginalImage] as? UIImage {
        img2.contentMode = .ScaleAspectFit
        img2.image = pickedImage2
    }

如您所见,我创建了 'img1' 和 'img2' 作为 imageView。 当我选择第二张图片时,它删除了 img1 以替换为 img2。 有了我的其余代码,我知道 'img2' = 'img2' 所以第二张图片在 'img2' 和 'img1' 消失了..我希望它在我看来.. 我该如何解决?

好吧,我专注于代码并找到了不同时间选择多张图像的方法,但在 view.

我只需要像这样转换我的 imagePickController 函数:

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
    if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {

        if (img1.hidden == true){


            imagePicker.allowsEditing = false
            imagePicker.sourceType = .PhotoLibrary


              img1.hidden = false
             img1.userInteractionEnabled = true
            img1.contentMode = .ScaleAspectFit
            img1.image = pickedImage


        }else if (img2.hidden == true) {


            imagePicker.allowsEditing = false
            imagePicker.sourceType = .PhotoLibrary


            img2.hidden = false
            img2.userInteractionEnabled = true
            img2.contentMode = .ScaleAspectFit
            img2.image = pickedImage

        } else {
            alert("STOP", message:"vous avez atteint la limite de photos possible")
        }


    }

    dismissViewControllerAnimated(true, completion: nil)
}