想要将图像直接从图像选择器传输到另一个具有uimageview的视图

时间:2019-04-19 12:40:51

标签: ios swift xcode uiimagepickercontroller

我有2个UIViewController,其中第一个UIViewController的导航栏中有一个按钮,在下一个控制器中,我有1个UIImageView 1个UITextField和1个UITextView。因此,当我单击第一个视图控制器上的按钮时,图像选择器应打开,并且当我选择任何图像时,图像选择器应直接传递到下一个视图控制器UIImageView。由于我是iOS的新手,所以不知道该怎么办

我的分镜脚本图片: storyboard image

我的第一个视图控制器,即 homeviewcontroller.swift 代码:

import UIKit
import GoogleMaps

class HomeViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate{

    @IBOutlet weak var mapView: GMSMapView!

    var zoom: Float = 15

    override func viewDidLoad() {
        super.viewDidLoad()

        let camera = GMSCameraPosition.camera(withLatitude: 21.2306, longitude: 72.8671, zoom: 6.0)
        mapView.camera = camera
        mapView.isMyLocationEnabled = true
        mapView.settings.myLocationButton = true
        showMarker(position: camera.target)


    }

    func showMarker(position: CLLocationCoordinate2D) {
        let marker = GMSMarker()
        marker.position = position
        marker.isDraggable = true
        marker.title = "GREWON TECHNOLOGIES PVT. LTD."
        marker.snippet = "SURAT, GUJARAT"
//        marker.appearAnimation = GMSMarkerAnimation.pop
        marker.map = mapView
    }

    @IBAction func imgPickerBtn(_ sender: Any) {
        let picker = UIImagePickerController()
        picker.delegate = self
        picker.allowsEditing = true
        picker.sourceType = .photoLibrary
        self.present(picker, animated: true, completion: nil)

    }

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

        if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {

        }
    }


    @IBAction func ZoomInBtn(_ sender: Any) {
        zoom = zoom + 1
        self.mapView.animate(toZoom: zoom)
    }

    @IBAction func zoomOutBtn(_ sender: Any) {
        zoom = zoom - 1
        self.mapView.animate(toZoom: zoom)
    }

}

extension HomeViewController:GMSMapViewDelegate {

    func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D) {
//        self.showMarker(position: coordinate)
    }

}

我的第二个视图,即 detailviewcontroller.swift

import UIKit

class DetailsViewController: UIViewController {

    var transferedImage:UIImage!

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

2 个答案:

答案 0 :(得分:1)

请参考以下代码:

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
        let objDetailsViewController = UIStoryboard(name: "storyboardName", bundle: nil).instantiateViewController(withIdentifier: "DetailsViewController_StoryboardID") as! DetailsViewController
        objDetailsViewController.transferedImage = pickedImage
        self.navigationController?.pushViewController(objDetailsViewController, animated: true)
    }
}

class DetailsViewController: UIViewController {
    var transferedImage:UIImage!
    @IBOutlet weak var imageView:UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()
        //Set your selected image
        imageView.image = transferedImage
}

如果为您工作,请标记为接受。

答案 1 :(得分:0)

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

        if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage 
        {
              if let secondVC = self.storyboard?.instantiateViewController(withIdentifier: "DetailViewController") as? DetailViewController {
                secondVC.transferedImage = pickedImage
                self.navigationController?.pushViewController(secondVC, animated: true)
              }

        }
}