从图书馆swift的视图中选择几个图像

时间:2016-08-20 22:24:25

标签: swift uiimagepickercontroller

我开始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'消失了......我想在我看来这两个... 我该怎么解决?

1 个答案:

答案 0 :(得分:0)

好的我专注于代码并找到了不同时选择多个图像的方法,但是在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)
}