Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

258
Visualizações
How to share image downloaded from firebase storage between different views?

This is the code i use to fetch the image from the storage. It works like charm but i dont want to redownload it on different views again.

func retreiveImagesFromFireBase(downloadUrl:String,imageHolder:UIImageView){
    if downloadUrl != "" {
        let storageRef = Storage.storage().reference(forURL: downloadUrl)
        // Download the data, assuming a max size of 1MB (you can change this as necessary)
        storageRef.getData(maxSize: 5*1024*1024) { (data, error) in
            if error == nil {
                if let pic = UIImage(data: data!) {
                    imageHolder.image = pic
                }
            } else {
                print(error)
            }
        }
    }
}

How should i handle this?

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

Just declare UIImage globally in your class, and inside your function, where you are setting imageHolder, assign that downloaded image to global image, and use it where ever you want.

UIImage image;

func retreiveImagesFromFireBase(downloadUrl:String,imageHolder:UIImageView){
            if downloadUrl != ""{
                let storageRef = Storage.storage().reference(forURL: downloadUrl)
                // Download the data, assuming a max size of 1MB (you can change this as necessary)
                storageRef.getData(maxSize: 5*1024*1024) { (data, error) in
                    if error == nil{
                        if let pic = UIImage(data: data!){
                            imageHolder.image = pic
                            image = pic // use it on other views 
                        }
                    }else{
                        print(error)
                    }
                }
            }
        }
over 4 years ago · Santiago Trujillo Relatório

0

If you have only one or two images then simply store in userdefault and then you can access in any viewController, Store image as data in userdefault whenever you want to use just convert data to image and use it, See the following code to convert data to image

let image = UIImage(data: imageData)
over 4 years ago · Santiago Trujillo Relatório

0

You can do

class Service { 
   static let shared = Service()  
   var image1:UIImage?
   var image2:UIImage?
   let imageLink = "//////" // or with SDWebImage
}

if let pic = UIImage(data: data!){
    Service.shared.image = pic
}

Or use SDWebImage and share the link , you can aslo make a global var like

var image:UIImage? 

but it's not recommended as it has no grouping to let the developer know it's source local/instance/global so it's confusing unlike singleton

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda