2

I am building an app that allows user to add three pictures to threeUIImageViews I added three buttons, for instance, when a user clicks on button one he should be able to add an image toUIImageView 1 but the image did not come to theUIImageView and I did not find any solution

sample of code:

 @IBAction func imageOneBtn(sender: AnyObject) {
        picker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
        picker.delegate = self
        picker.allowsEditing = false
        self.presentViewController(picker, animated: true, completion: nil)

        func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]){
            let selectedImage = info[UIImagePickerControllerOriginalImage] as! UIImage
            imageViewOne.image = selectedImage
            imageViewOne.contentMode = UIViewContentMode.ScaleAspectFit
            picker.dismissViewControllerAnimated(true, completion: nil)
        }
    }

Thank you in advance

Ashok Londhe
  • 1,491
  • 11
  • 29
user3599431
  • 43
  • 11

2 Answers2

0

The code seems to be correct.

let selectedImage = info[UIImagePickerControllerOriginalImage] as! UIImage

Try removing the exclamation mark (!).

Try using imageViewOne.contentMode = .ScaleAspectFit

Also try using var instead of let selectedImage

In general Try this code

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
var selectedImage = info[UIImagePickerControllerOriginalImage] as UIImage
imageViewOne.contentMode = .ScaleAspectFit
imageViewOne.image = selectedImage
dismissViewControllerAnimated(true, completion: nil)
}
DHEERAJ
  • 1,478
  • 12
  • 32
  • thank you for your answer but for the "!" mark I cannot remove it as I am using version 6.3 ... and I have changed the codes as you said but still :( is the problem that I can't use image picker function inside another method? in my case the button function?? – user3599431 Jun 03 '15 at 13:17
  • you cant write a method inside another method – DHEERAJ Jun 03 '15 at 13:21
0

I have solved my problem, and this is the answer for anyone who may want to use it

   import UIKit

 class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate  {


@IBOutlet weak var oneImg: UIImageView!
@IBOutlet weak var twoImg: UIImageView!
@IBOutlet weak var threeImg: UIImageView!

var num=0;

func config() {
    let imagePickerController = UIImagePickerController()
    imagePickerController.delegate = self
    imagePickerController.allowsEditing = true
    imagePickerController.sourceType = .PhotoLibrary
    presentViewController(imagePickerController, animated: true, completion: nil)
 }

 @IBAction func oneBtn(sender: AnyObject) {
    num = 1
    config()
 }


 @IBAction func twoBtn(sender: AnyObject) {
    num = 2
    config()
 }


 @IBAction func threeBtn(sender: AnyObject) {
    num = 3
    config()
 }


 func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]){
    let selectedImage = info[UIImagePickerControllerOriginalImage] as! UIImage;

    if num == 1 {  // switch can be used here
        oneImg.image = selectedImage;
     } else if (num == 2) {
        twoImg.image = selectedImage
     } else if (num == 3) {
        threeImg.image = selectedImage
     }
     picker.dismissViewControllerAnimated(true, completion: nil)
}

}
Badal Shah
  • 7,541
  • 2
  • 30
  • 65
user3599431
  • 43
  • 11