将图像从Firebase检索到UIimage swift5

编程入门 行业动态 更新时间:2024-10-17 23:21:24
本文介绍了将图像从Firebase检索到UIimage swift5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在努力将图片从Firebase存储下载到Swift 5中的UIImage.

I'm struggling to download pictures from firebase storage to an UIImage in swift 5.

我可以很好地上传它们.当我尝试检索图片时,UIImage显示黑屏.

I can well upload them. When I tried to retrieve picture, the UIImage display a black screen.

这是我的返回UIImage的函数

here my function which return the UIImage

import UIKit import Firebase func getImageEvent (imagePath : String) -> UIImage? { var myImage : UIImageView? //Access to the storage let storageRef = Storage.storage().reference(withPath: imagePath) storageRef.getData(maxSize: 1 * 1024 * 1024) {(data, error) in if let error = error { print(error.localizedDescription) return } if let data = data { print(data.description) myImage?.image = UIImage(data: data) } } return myImage?.image } //Call the function getImageEvent (imagePath :"9U4BoXgBgTTgbbJCz0zy/eventMainImage.jpg")

在控制台中,我可以很好地看到print(data.description)的值.

In the console, I can well see a value for print(data.description).

默认情况下,UIImageView中有一个图像.调用该函数时,默认图像将被黑屏取代.

By default, there is an image in the UIImageView. When call the function, the default image is replaced by a black screen.

您能帮我理解这个错误吗?

Could you please help me to understand the mistake ?

非常感谢

推荐答案

有很多解决方法,但首先对此问题进行简要说明:

There's a number of ways to go about this but a brief description of the issue first:

闭包中的return语句将在下载该映像之前执行-Firebase函数是异步的,必须以允许时间从Internet下载和获取数据的方式来编写代码.因此-不要尝试从异步函数返回数据.

The return statement within the closure will execute way before that image is downloaded - Firebase functions are asynchronous and code has to be crafted in a way that allows for time to download and get data from the internet. So - don't try to return data from asynchronous functions.

这是用完成处理程序重写的代码.只有在完全下载图像后,该处理程序才会被调用.

Here's the code re-written with a completion handler. That handler will be called only after the image is fully downloaded.

func getImageEvent (imagePath: String, completion: @escaping(UIImage) -> Void) { var myImage : UIImageView? let storageRef = Storage.storage().reference(withPath: imagePath) storageRef.getData(maxSize: 1 * 1024 * 1024) { data, error in if let error = error { print(error.localizedDescription) return } if let data = data { if let myImage = UIImage(data: data) { completion(myImage) } } } }

,关键是如何调用该函数.请注意,此代码等待数据(UIImage)在其关闭时传递回该数据,并让您知道获取图像已完成.

and the key is how to call that function. Note this code awaits the data (UIImage) to be passed back to it within it's closure and lets you know that getting the image was complete.

self.getImageEvent(imagePath: "9U4BoXgBgTTgbbJCz0zy/eventMainImage.jpg", completion: { theImage in print("got the image!") })

如果未下载图像或myImage为nil,则应添加其他错误检查.将错误消息与nil myImage一起传递回是一种选择,或者使对象作为可选传递回去,然后在 self.downloadImageAtPath 中检查nil将是另一种选择.

You should add additional error checking in case the image was not downloaded or myImage was nil. Passing back an error message along with the nil myImage would be one option, or making the object passed back as an optional and then checking for nil within self.downloadImageAtPath would be another.

更多推荐

将图像从Firebase检索到UIimage swift5

本文发布于:2023-11-26 13:31:59,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1634027.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图像   Firebase   UIimage

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!