[英]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.