Selecting photo returns to blank UI

2019-09-09 06:50发布

问题:

I am trying to make an image uploader on my ios app.

EDIT 7

This question is now more clearly asked here: Present Modal for Image Select Erases UI

END OF EDIT 7

The first thing I am trying to do is enable the user to select an image that they want for upload.

EDIT 6

I have simplified all the code to simply one button press. The Modal presentation deletes the previous page's UI. How I prevent this.

import UIKit

class AddImageToStandViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
    }


    @IBAction func loadImageButtonTapped(sender: UIButton) {
        let imagePicker = UIImagePickerController()
        imagePicker.allowsEditing = false
        imagePicker.sourceType = .PhotoLibrary

        self.presentViewController(imagePicker, animated: true, completion: nil)

    }
}

I have found that the selection works just fine and does not destroy the UI if I push/segue from a different to this controller.

END OF EDIT 6

Below this is the other methods that I have tried before this

So far, this code enables a user to press a button and choose a photo from their photos. But, after they click the photo it just returns them to an empty View except the navbar at the bottom.

It seems my user is being returned, not to the view that they came from, but to a separate empty view. How do I return the user to the view that they came from?

import UIKit

class CreateStandViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {


    @IBOutlet var image: UIImageView!

    ...


    @IBAction func selectPicture(sender: AnyObject) {

        let ImagePicker = UIImagePickerController()
        ImagePicker.delegate = self
        ImagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary

        self.presentViewController(ImagePicker, animated: true, completion: nil)

    }



    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {

        image.image = info[UIImagePickerControllerOriginalImage] as? UIImage
        self.dismissViewControllerAnimated(true, completion: nil)

    }

    ...

let me know if there is any more information that I should give.

EDIT 1

this code has the same effect

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {

    image.image = info[UIImagePickerControllerOriginalImage] as? UIImage
    picker.dismissViewControllerAnimated(true, completion: nil)


}

I have tried adding this line below but I get an error stating that CreateStandViewController.Type cannot be converted to the expected UIViewController.

self.presentViewController(CreateStandViewController, animated: true, completion: nil)

EDIT 2

This code below doesnt error but also changes nothing

    let createStandController = CreateStandViewController()
    self.presentViewController(createStandController, animated: true, completion: nil)

EDIT 3

I have literally copied all the code from here: http://www.codingexplorer.com/choosing-images-with-uiimagepickercontroller-in-swift/ and now have:

import UIKit

class CreateStandViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

    @IBOutlet var imageView: UIImageView!

    let imagePicker = UIImagePickerController()

    @IBAction func loadImageButtonTapped(sender: UIButton) {

        imagePicker.allowsEditing = false
        imagePicker.sourceType = .PhotoLibrary

        presentViewController(imagePicker, animated: true, completion: nil)
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        imagePicker.delegate = self
    }

    // MARK: - UIImagePickerControllerDelegate Methods

    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
        if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
            imageView.contentMode = .ScaleAspectFit
            imageView.image = pickedImage
        }

        dismissViewControllerAnimated(true, completion: nil)
    }

    func imagePickerControllerDidCancel(picker: UIImagePickerController) {
        dismissViewControllerAnimated(true, completion: nil)
    }

}

This has the same issue as before. When the image is selected I am sent back to a view that is white with the navbar at the bottom.

EDIT 4

I have run the same code in its own separate project as a stand alone view controller and it works. There is some code that I have in the background of my app that is killing the functionality of the code (maybe). I am guessing it may have something to do with the navbar?

EDIT 5

I removed the tab bar at the bottom by seguing to the image selecting view via a present Modally segue, but it had no effect.

回答1:

Just replace your

self.dismissViewControllerAnimated(true, completion: nil)

with

picker.dismissViewControllerAnimated(true, completion: nil)

I think you will get the point after that