图像未显示在imageView中
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了图像未显示在imageView中相关的知识,希望对你有一定的参考价值。
当我触摸一个按钮时没有任何反应,该按钮将我带到另一个VC,它应该从firebase获取图像,然后在按下一个名为loadImageFunc
的按钮后在VC上的图像视图中显示它
我的代码有什么问题?我没有错。它只是不起作用。
import UIKit
import FirebaseStorage
import FirebaseDatabase
import FirebaseAuth
import Firebase
class PhaseOneViewController: UIViewController {
@IBOutlet weak var p1ImageView: UIImageView!
@IBAction func loadImages(_ sender: Any) {
self.downloadImages(folderPath: "(Storage.storage().reference().child((Auth.auth().currentUser?.uid)!).child("post(takePicViewController().finalPost + PhotoArray.sharedInstance.numberPost)").child(ImageUploadManager().imageName))", success: { (img) in
self.p1ImageView.image = img
print(img)
}) { (error) in
print("here is errorrrrrrrrrrr", error)
}
}
func downloadImages(folderPath:String,success:@escaping (_ image:UIImage)->(),failure:@escaping (_ error:Error)->()){
// for i in 0 ..< 194 {
// Create a reference with an initial file path and name
let reference = Storage.storage().reference().child((Auth.auth().currentUser?.uid)!).child("post(takePicViewController().finalPost + PhotoArray.sharedInstance.numberPost)").child(ImageUploadManager().imageName)
reference.getData(maxSize: (3 * 1024 * 1024)) { (data, error) in
if let _error = error { //code fails here and prints the error
print(_error)
failure(_error)
} else {
if let _data = data {
let myImage:UIImage! = UIImage(data: _data)
success(myImage)
self.p1ImageView.image = myImage
}
}
}
//}
}
}
在按照下面的答案后我发现if else块的数据永远不会发生,因为代码在我上面指出的行上失败了(if语句的第一部分)它会产生错误
Domain = FIRStorageErrorDomain Code = -13010“Object DOFoO2mCg7bnZcbIdT7SFafquB3 / post1 / 153555392.86284.jpg不存在。” UserInfo = {object = DOFoO2mCg7bnZcbIdT7SFafFquB3 / post1 / 1535515392.86284.jpg,ResponseBody =
NoSuchKey
指定的密钥不存在。没有这样的对象:practicearraybasicimg1.appspot.com/DOFoO2Cg7bnZcbIdT7SFafFquB3/post1/1535515392.86284.jpg,buck = practicearraybasicimg1.appspot.com,data = <3c3f786d 6c207665 7273696f 6e3d2731 2e302720 656e636f 64696e67 3d275554 462d3827 3f3e3c45 72726f72 3e3c436f 64653e4e 6f537563 684b6579 3c2f436f 64653e3c 4d657373 6167653e 54686520 73706563 69666965 64206b65 7920646f 6573206e 6f742065 78697374 2e3c2f4d 65737361 67653e3c 44657461 696c733e 4e6f2073 75636820 6f626a65 63743a20 79756269 70726163 74696365 61727261 79626173 6963696d 67312e61 70707370 6f742e63 6f6d2f44 4f466f4f 326d4367 37626e5a 63624964 54375346 61664671 7542332f 706f7374 312f3135 33353531 35333932 2e383632 38342e6a 70673c2f 44657461 696c733e 3c2f4572 726f723e>,data_content_type = application / xml; charset = UTF-8,NSLocalizedDescription = Object DOFoO2mCg7bnZcbIdT7SFafFquB3 / post1 / 1535515392.86284.jpg不存在。,ResponseErrorDomain = com.google.HTTPStatus,ResponseErrorCode = 404}
更新:我还尝试在存储中设置我对以下内容的读写权限,如下面的回答所示。但是没有变化。
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read, write ;
}
}
}
更新:我现在尝试了以下代码:
override func viewDidLoad() {
super.viewDidLoad()
database = Database.database()
storage = Storage.storage()
let dbRef = database.reference().child("Posts").child(uid!).child("post(takePicViewController().finalPost + PhotoArray.sharedInstance.numberPost)")
dbRef.observe(.childAdded, withchildAdded: { (snapshot) in
// Get download URL from snapshot
let downloadURLsnapshot;.value() as! String
// Create a storage reference from the URL
let storageRef = storage.referenceFromURL(downloadURL)
// Download the data, assuming a max size of 1MB (you can change this as necessary)
storageRef.dataWithMaxSize(1 * 1024 * 1024) { (data, error) -> Void in
// Create a UIImage, add it to the array
let pic = UIImage(data: data)
picArray.append(pic)
};
})
}
@IBAction func loadImages(_ sender: Any) {
for image in picArray {
self.p1ImageView.image = image
}
}
但我明白了:
参数标签'(_:,withchildAdded :)'与任何可用的重载都不匹配
在dbRef.observe
线上......
试试这一个
let reference = Storage.storage().reference().child((Auth.auth().currentUser?.uid)!).child("post(takePicViewController().finalPost + PhotoArray.sharedInstance.numberPost)").child(ImageUploadManager().imageName)
reference.dataWithMaxSize(1 * 1024 * 1024) { (data, error) -> Void in
// Create a UIImage, from data
if let pic = UIImage(data: data){
print(pic)
}
})
Alexander,我认为你正在后台线程中更新UI。首先,如果是,则检查语句是否正在执行,然后尝试在主线程中执行此操作
DispatchQueue.main.async {
if let _data = data {
let myImage:UIImage! = UIImage(data: _data)
success(myImage)
self.p1ImageView.image = myImage
}
}
试试这种方式
let dbRef = database.reference().child("childName")
dbRef.observeEventType(.ChildAdded, withBlock: { (snapshot) in
// Get download URL from snapshot
let downloadURL = snapshot.value() as! String
// Create a storage reference from the URL
let storageRef = storage.referenceFromURL(downloadURL)
// Download the data, assuming a max size of 1MB (you can change this as necessary)
storageRef.dataWithMaxSize(1 * 1024 * 1024) { (data, error) -> Void in
// Create a UIImage, add it to the array
let pic = UIImage(data: data)
picArray.append(pic)
})
})
更新
dbRef.observe(.childAdded, with: { (snapshot) in
let downloadURL = snapshot.value() as! NSDictionary
...
})
只需更新withBlock即可
以上是关于图像未显示在imageView中的主要内容,如果未能解决你的问题,请参考以下文章
ImageView 未显示在 android 中使用相机捕获的图像