我正在尝试从数据库中检索保存的图片(在这种情况下,我从数据库中硬编码了想要的确切图片)。我启动了两个UIImage
变量:imageView
和rawImage
。 imageView正在使用SDWebImage
中的FirebaseUI
来加载图像,但是我不知道如何将其保存到UIImage数组中。
rawImage正在从Firebase的确切图像的路径中获取数据。我将其转换为UIImage,但是当我要将其附加到UIImage数组时,应用程序崩溃。
class GalleryViewController:UIViewController{
@IBOutlet weak var imageView: UIImageView!
@IBOutlet weak var rawImage: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
view.addVerticalGradientLayer(topColor: primaryColor, bottomColor: secondaryColor)
self.tabBarController?.tabBar.isHidden = true
retrievePicture()
}
func retrievePicture()
{
guard let user_email = Auth.auth().currentUser?.email else { return }
let storageRef = Storage.storage().reference()
// Reference to an image file in Firebase Storage
let reference = storageRef.child("\(user_email)").child("/top").child("1.jpg")
// UIImageView in your ViewController
let imageView: UIImageView = self.imageView
// Placeholder image
let placeholderImage = UIImage(named: "placeholder.jpg")
//UploadViewController.Clothing.top_images.append(<#UIImage#>)
// Load the image using SDWebImage
imageView.sd_setImage(with: reference, placeholderImage: placeholderImage)
// this is the code for the rawImage portion I described above
reference.getData(maxSize: 1 * 1024 * 1024) { data, error in
if let error = error {
print("Error \(error)")
} else {
let picture = UIImage(data: data!)
UploadViewController.Clothing.top_images.append(picture!)
}
}
// loading the image to the rawImage imageview
rawImage.image = UploadViewController.Clothing.top_images[0]
}
}
答案 0 :(得分:0)
如果您已经使用 SDWebImage 加载图像,则还可以将图像附加到 UIImage 数组中。
只需将 SDWebImage 与这样的完整块一起使用
x = 0
if(x < canvas.width - 50){
x+=7
}
console.log(x); // x = 7
答案 1 :(得分:0)
您已经忽略了强制展开可选选项,我建议您删除!
用法以避免崩溃,请改用以下命令
reference.getData(maxSize: 1 * 1024 * 1024) { data, error in
guard let data = data else {
print(error?.localizedDescription ?? "Check remote url or service")
return
}
if let picture = UIImage(data: data) {
UploadViewController.Clothing.top_images.append(picture)
} else {
print("Invalid image data\n\(String(data: data, encoding: .utf8))")
}
}
或在声明安全的情况下关注
if error == nil && data != nil, let picture = UIImage(data: data!) {
UploadViewController.Clothing.top_images.append(picture)
}
您也可以尝试使用不带斜杠的路径,如下所示:
let reference = storageRef.child("\(user_email)/top/1.jpg")