gpt4 book ai didi

ios - 如何将选定的图像从 UIImagePicker 传递到 UIViewController

转载 作者:行者123 更新时间:2023-11-29 00:05:38 25 4
gpt4 key购买 nike

我已经在我的应用程序中实现了一个 UIImagePicker,但我正在努力将 UIImage 传递给选择器所在的 UIViewController发起自(见下面代码中的注释)

下面的代码是存储在 Extensions/ImageUploader.swift

中的扩展
import UIKit
import Firebase

extension UIViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate{


public func ImagePicker() {
let picker = UIImagePickerController()
picker.delegate = self
picker.allowsEditing = true
// mediaType is image only by default

present(picker, animated: true, completion: nil)

}


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

var selectedImageFromPicker: UIImage?

if let editedImage = info[UIImagePickerControllerEditedImage] as? UIImage {
selectedImageFromPicker = editedImage
}
if let originalImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
selectedImageFromPicker = originalImage
}

picker.dismiss(animated: true, completion:{print("Image upload started")})// Closing the window before the upload, so upload can continue in background while user finishes the form

// Here I want to pass the selectedImageFromPicker to the ViewController that initiated the imagePicker - how do I do that??


})
}
}

createActivityVC 中,我有这个 IBAction 来访问 photo library 并启动选择器 - self.imagePicker()

@IBAction func addPictureBtnWasPressed(_ sender: Any) {

PHPhotoLibrary.requestAuthorization({(status:PHAuthorizationStatus) in
switch(status) {

case .notDetermined:

// Access has not been determined.
PHPhotoLibrary.requestAuthorization({ (newStatus) in

if (newStatus == PHAuthorizationStatus.authorized) {
// Access has been granted.

self.imagePicker()
}

else {

}
})
break

case .restricted:
break

case .denied:
// Access has been denied.
print("Acccess to photo denied")
break

case .authorized:
// Access has been granted.
print("Access to photo authorized")

self.imagePicker()

break


}
})
}

我想将 selectedImageFromPicker 传递给 viewController createActivityVC。如何才能做到这一点?我宁愿不专门命名 viewController,而是让它是动态的,这样 imagePicker 就可以在可能的情况下跨多个 ViewController 使用。

最佳答案

在您的表单中,您是否实现了 UIImageView?如果是这样的话,在你的

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

你可以说:

myFormImageView.image = selectedImageFromPicker

然后当您提交表单时,您可以上传表单信息+图像数据

更新:

我通过子类化 UIIMagePickerController 的方法

import UIKit

protocol ImagePickerDelegate {
func imagePicked(image: UIImage)
}

class myPickerViewController: UIImagePickerController, UIImagePickerControllerDelegate, UINavigationControllerDelegate{

var imagePickerDelegate:ImagePickerDelegate!

override func viewDidLoad() {
super.viewDidLoad()
allowsEditing = true
delegate = self
}

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let selectedImage = info[UIImagePickerControllerOriginalImage] as! UIImage
imagePickerDelegate.imagePicked(image: selectedImage)
dismiss(animated: true, completion: nil)
}

}

class ViewController : UIViewController, ImagePickerDelegate {

func imagePicked(image: UIImage) {
imagePicked = image
}


var imagePicked:UIImage! {
didSet {
imageView.image = imagePicked
}
}

let button: UIButton = {
let button = UIButton()
button.translatesAutoresizingMaskIntoConstraints = false
button.setTitle("Select an image", for: .normal)
button.backgroundColor = .red
button.addTarget(self, action: #selector(handleImagePicker), for: .touchUpInside)
return button
}()

let imageView: UIImageView = {
let iv = UIImageView()
iv.translatesAutoresizingMaskIntoConstraints = false
iv.contentMode = .scaleAspectFill
iv.layer.masksToBounds = true
iv.layer.borderWidth = 2
iv.layer.borderColor = UIColor.black.cgColor
return iv
}()

override func viewDidLoad() {
super.viewDidLoad()

title = "Get image via Delegate"
view.backgroundColor = .white

view.addSubview(button)
view.addSubview(imageView)

button.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 100).isActive = true
button.heightAnchor.constraint(equalToConstant: 30).isActive = true
button.widthAnchor.constraint(equalToConstant: 150).isActive = true
button.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true

imageView.topAnchor.constraint(equalTo: self.button.topAnchor, constant: 50).isActive = true
imageView.leftAnchor.constraint(equalTo: self.view.leftAnchor, constant: 20).isActive = true
imageView.rightAnchor.constraint(equalTo: self.view.rightAnchor, constant: -20).isActive = true
imageView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: -50).isActive = true

}

@objc func handleImagePicker() {
let myPickerVC = myPickerViewController()
myPickerVC.imagePickerDelegate = self

self.present(myPickerVC, animated: true, completion: nil )
}

}

你创建一个新项目并粘贴它会工作的整个代码,我想,就像我一样。希望对你有帮助

关于ios - 如何将选定的图像从 UIImagePicker 传递到 UIViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48138529/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com