gpt4 book ai didi

ios - 如何分离这两个功能?

转载 作者:行者123 更新时间:2023-11-28 15:31:06 25 4
gpt4 key购买 nike

现在,当我点击选择图片操作时,我选择了一张图片,一旦图片选择器关闭,它就会直接进入数据库。如何使它具有两种不同的功能,以便一个按钮选择一张图片,另一个按钮上传它?

@IBAction func pickImage(_ sender: Any) {


let image = UIImagePickerController()
image.delegate = self
image.sourceType = UIImagePickerControllerSourceType.photoLibrary

image.allowsEditing = false

self.present(image, animated: true)
{
//after its completed
}


@objc(imagePickerController:didFinishPickingMediaWithInfo:) func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])
{
if let image = info[UIImagePickerControllerOriginalImage] as? UIImage
{
myImageView.image = image

}
else
{
//error
}

self.dismiss(animated: true, completion: nil)


let storageRef = Storage.storage().reference().child("images/\(NSUUID().uuidString)/image.png")
if let uploadData = UIImagePNGRepresentation(self.myImageView.image!){

storageRef.putData(uploadData, metadata: nil, completion:
{
(metadata, error) in
if error != nil {
print("error")
return
}

else {

let downloadURL = metadata?.downloadURL()?.absoluteString
self.ref?.child("Posts").childByAutoId().setValue(["Title": self.titleText.text,"Article": self.mainText.text, "Author": self.authorText.text, "Date": self.dateText.text, "Download URL": (downloadURL)])

}
}
)
}

最佳答案

从您当前的代码看来,您已经在 pickImage 按钮操作中添加了 UIImagePickerController 的委托(delegate)方法,这是错误的委托(delegate)方法,也应添加为您的其他类实例方法意味着它必须在 pickImage 按钮之外。

@IBAction func pickImage(_ sender: Any) {

let image = UIImagePickerController()
image.delegate = self
image.sourceType = UIImagePickerControllerSourceType.photoLibrary
image.allowsEditing = false

self.present(image, animated: true)
}

//Add didFinishPickingMediaWithInfo here
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
myImageView.image = image
}
else {
//error
}
self.dismiss(animated: true, completion: nil)
}

现在添加新的按钮操作 uploadImage 并添加 FIRStorage 的代码。之后只需将此操作与您的上传按钮绑定(bind)即可。

@IBAction func uploadImage(_ sender: Any) {

let storageRef = Storage.storage().reference().child("images/\(NSUUID().uuidString)/image.png")
if let uploadData = UIImagePNGRepresentation(self.myImageView.image!){

storageRef.putData(uploadData, metadata: nil, completion: { (metadata, error) in
if error != nil {
print("error")
return
}
else {
let downloadURL = metadata?.downloadURL()?.absoluteString
self.ref?.child("Posts").childByAutoId().setValue(["Title": self.titleText.text,"Article": self.mainText.text, "Author": self.authorText.text, "Date": self.dateText.text, "Download URL": (downloadURL)])
}
})
}
}

关于ios - 如何分离这两个功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44731992/

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