2

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?

  • http://stackoverflow.com/a/20756957/6297658 – Dravidian Aug 21 '16 at 04:26
  • thank you, i ll try to use ELCImagePickerController. but xcode say "use of unresolved identifier "ELCImagePickerController". have i to import something more? – Matt Jacquet Aug 21 '16 at 07:23
  • You are writing in swift right? `ELCImagePickerController` is a obj-C written cocoapod , use `DKImagePickerController ` for swift Or you can use a `Bidging Header` for `ELCImagePickerController` – Dravidian Aug 21 '16 at 07:31
  • i cant find it in package manager... and it pick several pictures in the same time ,right? i just want show several picture in my viewController. – Matt Jacquet Aug 23 '16 at 11:45

1 Answers1

0

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