我尝试将两个不同的图像同时保存在一个存储位置
这是我保存信息的功能
var text: String = ""
var addedByUser: String?
var userImage: UIImage?
var jobImage: UIImage!
var downloadURL: String?
var userDownloadURL: String?
let ref: DatabaseReference!
init(text: String? = nil, jobImage: UIImage? = nil, addedByUser: String? = nil, userImage: UIImage? = nil) {
self.text = text!
self.jobImage = jobImage
self.addedByUser = addedByUser
self.userImage = userImage
ref = Database.database().reference().child("jobs").childByAutoId()
}
init(snapshot: DataSnapshot){
ref = snapshot.ref
if let value = snapshot.value as? [String : Any] {
text = value["text"] as! String
addedByUser = value["addedByUser"] as? String
downloadURL = value["imageDownloadURL"] as? String
userDownloadURL = value["imageUserDownloadURL"] as? String
}
}
func save() {
let newPostKey = ref.key
// save jobImage
if let imageData = jobImage.jpegData(compressionQuality: 0.5) {
let storage = Storage.storage().reference().child("jobImages/\(newPostKey)")
storage.putData(imageData).observe(.success, handler: { (snapshot) in
self.downloadURL = snapshot.metadata?.downloadURL()?.absoluteString
let postDictionary = [
"imageDownloadURL" : self.downloadURL!,
"imageUserDownloadURL" : self.userDownloadURL!,
"text" : self.text,
"addedByUser" : self.addedByUser!
] as [String : Any]
self.ref.setValue(postDictionary)
})
}
}
我尝试了以下代码
if let imageData = jobImage.jpegData(compressionQuality: 0.5), ((userImage?.jpegData(compressionQuality: 0.5)) != nil) {
但是它不起作用,因为什么也没保存在数据库中...
您对我如何解决有任何想法?
答案 0 :(得分:0)
我相信问题是如何将图像上传到两个不同的位置。目前尚不清楚为什么有观察功能,因此该答案忽略了它,因为可能不需要。
从代码开始,保存功能将如下所示:
func save() {
self.uploadImageTask(imageName: "my_image.png", toLocation: "jobImage")
self.uploadImageTask(imageName: "my_image.png", toLocation: "anotherLocation")
}
然后是上传功能
func uploadImageTask(imageName: String, toLocation: String) {
let theImage = UIImage(named: imageName) //set up your image here
let data = UIImagePNGRepresentation(theImage)! //we're doing a PNG
let storage = Storage.storage()
let storageRef = storage.reference()
let locationRef = storageRef.child("images").child(toLocation)
let imageLocationRef = locationRef.child(imageName)
// Upload the file to the path "images/location/imageName"
let uploadTask = locationRef.putData(data, metadata: nil) { (metadata, error) in
guard let metadata = metadata else {
print("error while uploading")
return
}
let size = metadata.size // Metadata contains file metadata such as size, content-type.
print(size)
locationRef.downloadURL { (url, error) in
guard let downloadURL = url else {
print("an error occured after uploading and then downloading")
return
}
let x = downloadURL.absoluteString
print(x) //or build a dict and save to Firebase
}
}
}
结果是一张图像存储在
/images/jobImage/my_image.png
/images/anotherLocation/my_image.png
,它还将打印每个图像的路径,该路径可以存储在Firebase中。
答案 1 :(得分:-1)
let refDatabase = Database.database().reference()
let refstorage = Storage.storage().reference()
let data = image.jpegData(compressionQuality: 1.0) //
let metadata = StorageMetadata()
metadata.contentType = "image/jpeg"
let postkey = refDatabase.child("Post").childByAutoId().key
let imagename = "PostImage/\(postkey).jpeg"
refstorage = refstorage.child(imagename)
let timestamp = Date().timeIntervalSince1970 // you can use this to track time when it was uploaded.
self.refstorage?.putData(data!, metadata: metadata, completion: { (meta, error) in
if error == nil{
if let imageData = meta?.downloadURL()?.absoluteString{
let postDictionary = [
"imageDownloadURL" : self.downloadURL!,
"imageUserDownloadURL" : self.userDownloadURL!,
"text" : self.text,
"addedByUser" : self.addedByUser!
] as [String : Any]
self.refDatabase.child("Image").child(postkey).updateChildValues(postDictionary)
} else {
print("Couldn't get the URL for image")
}
}
})
我不确定Firebase数据库的外观,因此请相应地更改键。