简体   繁体   English

swift 我想将从图库中选择的图像传递到下一个视图 controller 作为链接

[英]swift i want to pass image selected from gallery to next view controller as link

hey everyone i'm new to swift.大家好,我是 swift 的新手。 i just pass the picked image from gallery to the next view controller as a link.我只是将选择的图像从画廊传递到下一个视图 controller 作为链接。 heres some code --继承人一些代码 -

 @objc func imageTapped(tapGestureRecognizer: UITapGestureRecognizer)
{
    print("image tapped")
    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.photoLibrary){
        print("Button capture")
        let imag = UIImagePickerController()
        imag.delegate = self
        imag.sourceType = UIImagePickerControllerSourceType.photoLibrary;
        imag.allowsEditing = true
        self.present(imag, animated: true, completion: nil)
    }
}

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
        uploadImage.image = pickedImage
    
    }
    picker.dismiss(animated: true, completion: nil)
}

now i have the selected image in "pickedImage".现在我在“pickedImage”中有选定的图像。 and on click next button然后点击下一步按钮

    @IBAction func onClickNext(_ sender: Any) {
        let controller = self.storyboard?.instantiateViewController(withIdentifier: "secondViewController") as! SecondViewController
        self.navigationController?.pushViewController(controller, animated: true)
}

how to pass this image (pickedImage) to next view controller如何将此图像(pickedImage)传递到下一个视图 controller

In your case it seems the easiest would be to add a property on your SecondViewController which accepts your image.在您的情况下,似乎最简单的方法是在您的SecondViewController上添加一个接受您的图像的属性。 So something like所以像

class SecondViewController: UIViewController {
    
    var inputImage: UIImage?
    ...

Now you can use this property when you create the view controller and pass it to your navigation controller.现在,您可以在创建视图 controller 并将其传递给导航 controller 时使用此属性。

@IBAction func onClickNext(_ sender: Any) {
    let controller = self.storyboard?.instantiateViewController(withIdentifier: "secondViewController") as! SecondViewController
    controller.inputImage = uploadImage.image
    self.navigationController?.pushViewController(controller, animated: true)
}

use above answer for navigation.使用上面的答案进行导航。 For url in didfinishPickingMediainfo:对于 didfinishPickingMediainfo 中的 url:

 func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
    print("I am in dismiss")

    if let image = info[UIImagePickerController.InfoKey.editedImage] as? UIImage {
        //            self.addImage.image = image
        let imageUrl          = info[UIImagePickerController.InfoKey.imageURL] as? NSURL
        var imageName         = imageUrl?.lastPathComponent
        guard let imageData = image.jpegData(compressionQuality: 0.75) else { return }

        
        
    }else{
        if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
            //            self.addImage.image = image
            let imageUrl          = info[UIImagePickerController.InfoKey.imageURL] as? NSURL
            var imageName         = imageUrl?.lastPathComponent
            guard let imageData = image.jpegData(compressionQuality: 0.75) else { return }
            //                let data = UIImageJPEGRepresentation(image, 0.2) as Data?

            
            
        }
    }
    
    picker.dismiss(animated: true, completion: nil);
}

check the above code its for both Camera and Gallery image.检查上面的代码它的相机和画廊图像。 check "imageUrl" variable检查“imageUrl”变量

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何快速将NSDictionary传递给下一视图控制器 - How to pass NSDictionary to next view controller in swift 如何将选定的图像从ImagePickerController传递到第二个视图控制器? - How to pass a selected image from ImagePickerController to second view controller? 使用Firebase将图像从一个视图控制器传递到另一个(swift) - pass an image from one view controller to another (swift) with Firebase Xcode图像将ImageView链接到下一个View Controller - Xcode image link ImageView into next View Controller 如何将多个选定的表视图单元格数据传输到Swift中的下一个视图控制器? - How do I transfer multiple selected table view cells data to the next view controller in Swift? 如何在IOS 9(Swift)中将选定的联系人显示到下一个View Controller - How to display selected contacts into next View Controller in IOS 9(Swift) Swift / XCode-为什么在按下tableViewCell时不能将数据传递给下一个视图控制器? - Swift/XCode - Why can't I pass data to the next view controller when a tableViewCell is pressed? 将多个选定表单元格中的数组传递到下一个视图控制器 - Pass the array in multiple selected table cell into next view controller 将数据从选定行传递到视图控制器 - pass data from selected row to view controller 从图库中选择图像后如何打开下一个视图控制器(swift) - How to open next viewcontroller after choosing image from gallery (swift)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM