Matt Jacquet Matt Jacquet - 3 months ago 13
Swift Question

pick several image in a view from library swift

I begin swift and I wanted to pick some images from the iPhone library to some UIImageViews.
I used the followed code but my problem is each time I pick a new image, it placed on the previous UIImageView, not in it own 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
}


AS you can see, i created 'img1' and 'img2' as imageView.
when I selected the 2nd image , it delete img1 to replace by img2.
with thte rest of my code, i know 'img2' = 'img2' so the 2nd image is in 'img2' and 'img1' faded away.. i want it both in my view..
how can I fix it?

Answer

Ok i focused myself on the code and find the way to pick several images not in the same time, but in the view.

I just have to transform my imagePickController function like that:

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)
}