swift - Retrieving firebase Image into ImageView

牧云@^-^@ 提交于 2021-02-11 14:13:58

问题


Hello everyone I am struggling with retrieving my image from firebase into my Image view. The first thing I do is create a method to retrieve the photo data in an array here is that code:


class PhotoService {
    
    static func retrievePhotos(completion: @escaping ([Photo]) -> Void) {
        
        //get a databas refrence
        let db = Firestore.firestore()
        
        //get data from "photos" collection
        db.collection("photos").getDocuments { (snapshot, Error) in
            
            //check for errors
            if Error != nil {
                //error in retrieving photos
                return
            }
            
            //get all the documents
            let documents = snapshot?.documents
            
            //check that documents arent nil
            if let documents = documents {
                
                //create an array to hold all of our photo structs
                var photoArray = [Photo]()
                
                //loop through documents, get a photos struct for each
                for doc in documents {
                    
                    //create photo struct
                    let p = Photo(snapshot: doc)
                    
                    if p != nil {
                        
                        //store it in an array
                        photoArray.insert(p!, at: 0)
                        
                    }
                    
                }
                
                //pass back the photo array
                completion(photoArray) 
                
            }
            
        }
        
    }

Then I call that class and attempt to display the Image in the image view as so:


@IBOutlet var profilePictureImageView: UIImageView!
        
    var photos = [Photo]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
                
        //call the photo service to retrieve the photos
        PhotoService.retrievePhotos { (retrievedPhotos) in
            
            //set the photo array to the retrieved photos
            self.photos = retrievedPhotos
            
            //make the image view a circle
            self.profilePictureImageView.layer.cornerRadius = self.profilePictureImageView.bounds.height / 2
            self.profilePictureImageView.clipsToBounds = true
            
            //make the image view display the photo
            var photo:Photo?
            
            func displayPhoto(photo:Photo) {
                
                //check for errors
                if photo.photourl == nil {
                    return
                }
                
                //download the image
                let photourl = URL(string: photo.photourl!)
                
                //check for erorrs
                if photourl == nil {
                    return
                }
                
                //use url session to download the image asynchronously
                let session = URLSession.shared
                
                let dataTask = session.dataTask(with: photourl!) { (data, response, Error) in
                    
                    //check for errors
                    if Error == nil && data != nil {
                        
                        //let profilePictureImageView = UIImage()
                        let image = UIImage(data: data!)
                        
                        //set the image view
                        DispatchQueue.main.async {
                            self.profilePictureImageView.image = image
                        }
                        
                    }
                    
                }
                
                dataTask.resume()  
            }
            }
        }
}

Not sure what I am doing wrong, If anyone can tell me what I am doing wrong or what I need to add I would really appreciate it Thank you!

来源:https://stackoverflow.com/questions/62647467/swift-retrieving-firebase-image-into-imageview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!