FactorJose
FactorJose

Reputation: 491

Allows editing UIImagePickerViewController doesn't work picking 2 different images

I have a view with 2 UIImageViews and 2 buttons in order to select 2 different pictures with UIImagePickerViewController. It actually works perfect but I would like to zoom the images and finally show the selected part of the image in the UIImageViews.

Allows editing works correctly with zoom and it apparently ''allows to edit' the pictures, but in the end the selected part is not presented in the UIImageViews .

Code is here. What is wrong?

import UIKit

var fotoUnoEscogida = UIImage()
var fotoDosEscogida = UIImage()


class viewControllerDso : UIViewController, UIImagePickerControllerDelegate ,UINavigationControllerDelegate, UITextViewDelegate{


    //Images
    @IBOutlet weak var imageView1: UIImageView!
    @IBOutlet weak var imageView2: UIImageView!
    //TextViews
    @IBOutlet weak var textViewExteriorUno: UITextView!
    @IBOutlet weak var textViewExteriorDos: UITextView!
    @IBOutlet weak var textViewInteriorUno: UITextView!
    @IBOutlet weak var textViewInteriorDos: UITextView!




    var imagePicker = UIImagePickerController()
    var imagePicked = 0

    override func viewDidLoad() {
        super.viewDidLoad()



    }


    @IBAction func chooseImage(sender: UIButton) {
        if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.SavedPhotosAlbum){
            print("chooseImage Button capture ")

            imagePicked = sender.tag
            imagePicker.delegate = self
            imagePicker.sourceType = .SavedPhotosAlbum
            imagePicker.allowsEditing = true

            self.presentViewController(imagePicker, animated: true, completion: nil)
        }
    }
    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
        let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage

        if imagePicked == 1 {

            imageView1.contentMode = UIViewContentMode.ScaleAspectFill
            imageView1.image = pickedImage

            imageView1.layer.masksToBounds = true
            imageView1.layer.borderWidth = 6
            imageView1.layer.cornerRadius = imageView1.layer.frame.height/6
            imageView1.layer.borderColor = UIColor.purpleColor().colorWithAlphaComponent(0.2).CGColor




        } else if imagePicked == 2 {


            imageView2.contentMode = UIViewContentMode.ScaleAspectFill
            imageView2.image = pickedImage

            imageView2.layer.masksToBounds = true
            imageView2.layer.borderWidth = 6
            imageView2.layer.cornerRadius = imageView2.layer.frame.height/6
            imageView2.layer.borderColor = UIColor.purpleColor().colorWithAlphaComponent(0.2).CGColor




        }
        dismissViewControllerAnimated(true, completion: nil)
    }


}

Upvotes: 0

Views: 250

Answers (2)

vatana chhorn
vatana chhorn

Reputation: 19

Update Swift 5.2

let pickedImage = info[UIImagePickerController.InfoKey.editedImage]

Upvotes: 0

Rahul Patel
Rahul Patel

Reputation: 1832

in didFinishPickingMediaWithInfo instead of

let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage

use

let pickedImage = info[UIImagePickerControllerEditedImage] as? UIImage

Upvotes: 2

Related Questions