Retrieve firebase Image - ImageView - swift

Hello everyone I am trying to take my image from the firebase database and present it into an image view as the profile image of the user. The first thing I do is create a method to retrieve the photo data in an array
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
#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()
}
}
}
}
I dont understand what I am doing wrong and why the image is not showing up if anyone can explain this to me and give me an example with code that would be amazing, explain it as though you are explaining it to a kid in grade 5

A quick way to get Image from Firebase and assigning it to an ImageView can be done easily in these Steps.
Function to Get PhotoUrl
//Function to get photo of loggedin User
func getUrl(Completion:#escaping((String)->())) {
let userID = Auth.auth().currentuser?.uid
let db = Firestore.firestore().collection("photos").document(userID)
db.getDocument { (docSnapshot, error) in
if error != nil {
return
} else {
guard let snapshot = docSnapshot, snapshot.exists else {return}
guard let data = snapshot.data() else {return}
let imageUrl = data["photoUrl"] as! String
completion(imageUrl)
}
}
}
To download image and assign it to image view
//Call this function in VC where your `ImageView` is
func getImage(Url:String){
DispatchQueue.global().async {
let url = URL(string: Url)
if let data = try? Data(contentsOf: url!) {
DispatchQueue.main.async {
self.profilePictureImageView.image = UIImage(data: data)
}
}
}
}
}
Call these inside viewDidLoad like this:
getUrl{(url) in
getImage(Url:url)
}

Related

How to load image from Firebase into users avatar

I have a problem with loading images from firebase. I have two functions. One of them collect info about user, second one load users avatar image. Unfortunately images load after function creates new user. I know it will be problem with asynchronous of Firebase but I don't know how to set up DispatchQueue to work properly. Can you help me with that?
// function that load user image in user manager class
func loadUserImage(contactUserID: String, completion: #escaping (UIImage) -> Void) {
let userID = Auth.auth().currentUser!.uid
var userImageRef = self.storage.child("\(userID)/userImage.jpg")
var image = UIImage()
if contactUserID != "" {
userImageRef = self.storage.child("\(contactUserID)/userImage.jpg")
}
userImageRef.getData(maxSize: 5 * 1024 * 1024) { (data, error) in
if let error = error {
print("Error with retrieving data: \(error.localizedDescription)")
} else {
if data?.count != 0 {
image = UIImage(data: data!)!
} else {
image = UIImage(systemName: "person.circle.fill")!
}
completion(image)
}
}
}
// function that load user in contact manager class
func loadContactList(completion: #escaping ([User]) -> Void) {
let currentUserID = Auth.auth().currentUser!.uid
db.collection("contacts")
.document(currentUserID)
.collection("userContacts")
.addSnapshotListener { (querySnapshot, error) in
var contactList = [User]()
if let error = error {
print("Error with retrieving data from DB: \(error.localizedDescription)")
} else {
if let snapshotDocuments = querySnapshot?.documents {
for document in snapshotDocuments {
let data = document.data()
let uid = data["uid"] as! String
let name = data["name"] as! String
let email = data["email"] as! String
var contact = User(email: email, name: name, userID: uid)
DispatchQueue.global().sync {
self.userService.loadUserImage(contactUserID: uid) { (image) in
contact.photoURL = image
}
}
contactList.append(contact)
contactList.sort {
$0.name < $1.name
}
completion(contactList)
}
}
}
}
}
// Function implementation in viewController
func loadContactList() {
self.contactService.loadContactList { (contactArray) in
self.contactList = contactArray
self.tableView.reloadData()
}
}
What you can do is to store the image url in the firebase database and after that create this extension:
import UIKit
let imageCache: NSCache = NSCache<AnyObject, AnyObject>()
extension UIImageView {
func loadImageUsingCacheWithUrlString(urlString: String) {
self.image = nil
if let cachedImage = imageCache.object(forKey: urlString as AnyObject) as? UIImage {
self.image = cachedImage
return
}
let url = URL(string: urlString)
if let data = try? Data(contentsOf: url!) {
DispatchQueue.main.async(execute: {
if let downloadedImage = UIImage(data: data) {
imageCache.setObject(downloadedImage, forKey: urlString as AnyObject)
self.image = downloadedImage
}
})
}
}
}
And call:
if let url = data["imgUrl"] as? String {
self.myImageView.loadImageUsingCacheWithUrlString(urlString: url)
}
For that what you need to do is to create and initialize an UIImage object. If you are working with cell classes you need to create this object in the cell.

Swift: upload multiple images to Firestore and assign URLs to object

I'm making an app whereby users post 2 images. I'm using Firebase for storage and as my database.
In my method to upload the images what I had wanted to do was to essentially use this method to return the URLs separately as well. I had written the following:
private func uploadImage(image: UIImage) -> URL? {
let randomName = UUID()
let storageRef = storage.reference().child("\(randomName)/png")
guard let uploadData = image.pngData() else { return nil}
var imageUrl: URL?
storageRef.putData(uploadData, metadata: nil) { (metadata, error) in
if error != nil {
print(error?.localizedDescription)
return
}
storageRef.downloadURL { (url, error) in
if error != nil {
print(error?.localizedDescription)
} else {
imageUrl = url
}
}
}
return imageUrl
}
And then I wrote the following 'post' method which is run when the submit button is tapped:
#objc func post() {
if let question = questionText.text,
let hashtagText = hashtagTextField.text,
let userHandle = Auth.auth().currentUser?.email,
let firstImage = left.image,
let secondImage = right.image,
let firstImageURL = uploadImage(image: firstImage)?.absoluteString,
let secondImageURL = uploadImage(image: secondImage)?.absoluteString
{
db.collection("posts").addDocument(data: [
"firstImage" : firstImageURL,
"secondImage" : secondImageURL,
"question" : question,
"hashtagText" : hashtagText,
"userHandle" : userHandle
]) { (error) in
if let e = error {
print("There was an issue saving data to Firestore, \(e)")
} else {
print("Successfully saved data")
self.dismiss(animated: true, completion: nil)
}
}
}
}
However, obviously the first method is not going to work as the closure is run after imageUrl is returned, therefore returning nil.
I've been trying to figure out how to manage this scenario - I had considered using a loop to populate an array of images but this got messy and I'm sure it is not the standard way to handle this. Any help would be greatly appreciated.
The return imageUrl is in the wrong place. It will return before Firebase has had time to store the image and return the url.
Additionally, the name of the file is not going to work. You currently have
storage.reference().child("\(randomName)/png") // xxxxx/png?
when it should be
storage.reference().child("\(randomName).png") // xxxxx.png
You can't 'return' data from a Firebase closure because firebase is asynchronous - a completion handler may possibly be a solution, but we don't know what the total use case is.
Let's assume you want want to store a users vacation picture in storage and then store that url in Firestore
private func uploadImage(image: UIImage) {
guard let uid = Auth.auth().currentUser?.uid else { return } //this users uid
let storageRef = storage.reference().child(uid).child("vacation.png")
//the path will be storage/users uid/vacation.png
guard let uploadData = image.pngData() else { return nil}
storageRef.putData(uploadData, metadata: nil) { (metadata, error) in
if error != nil {
print(error?.localizedDescription)
return
}
storageRef.downloadURL { (url, error) in
if error != nil {
print(error?.localizedDescription)
} else {
if url != nil {
//it's here where we store the imageUrl in Firestore
let dict = ["theUrl": url?.absoluteURL)]
let userRef = self.db.collection("users").document(uid)
//self.db points to *my* Firestore
userRef.collection("my_pics").addDocument(data: dict)
//will store in firstore/users/uid/docId/theUrl: the url
}
}
}
}
}

How can I download an array of url images from the web

Using regular expressions, I extracted the html string, the desired images and put them in an array.
What does the array look like at the moment:
var collectionPhotoLinks = [
"http:static-cdn3.vigbo.tech/u65463/78125/blog/5162255/4423863/57114004/500-codleto-889a051259ba894edee37b8da63ddbe9.jpg",
"http:static-cdn3.vigbo.tech/u65463/78125/blog/5162255/4423863/57114004/500-codleto-9516ab3514e07bde176f117e70c7ba85.jpg"]
I can upload one image
func downloadImages () {
guard let url = URL(string:
"http:static-cdn3.vigbo.tech/u65463/78125/blog/5162255/4423863/57114004/500-codleto-889a051259ba894edee37b8da63ddbe9.jpg")
else { return }
URLSession.shared.dataTask(with: url) { (data, response, error) in
if let data = data, let image = UIImage(data: data) {
DispatchQueue.main.async {
self.imageView.image = image
}
}
} .resume()
}
If the "url" is replaced with an array "collectionPhotoLinks", then respectively xcode requires string not array
So the question follows, how do I load all the images from the array? In the future I will need to send them to tableview or collectionview, but I would like to resolve the issue with this first.
The usual is that you download it inside cellForRowAt with say SDWebImage , but if you need to pre-download all then you can try
let arr = ["url1","urls"]
func downloadImages () {
let g = DispatchGroup()
for item in arr {
guard let url = URL(string:item)
else { return }
g.enter()
URLSession.shared.dataTask(with: url) { (data, response, error) in
if let data = data, let image = UIImage(data: data) {
//
}
g.leave()
} .resume()
}
g.notify(queue: .main) {
// done
}
}

Swift 3 - How to download Profile Image from FireBase Storage

I need help on retrieving Image from firebase storage, i learned how to save it but cant download it to current user profile.
here is my code so far:
FIRAuth.auth()?.createUserWithEmail(email!, password: password!, completion: { (authData, error) in
if error == nil {
if (password == "" || name == "" || email == "") {
self.showAlert("error", message: " Please Fill In The Blank")
}
if (password != confirm_password) {
self.showAlert("ERROR", message: "Password Don't Match")
}
}
else {
self.showAlert("ERROR", message: "Please Try Again")
}
let filePath = "\(FIRAuth.auth()!.currentUser!.uid)/\("userPhoto")"
var data = NSData()
let metaData = FIRStorageMetadata()
//let imageName = NSUUID().UUIDString
let storageRef = FIRStorage.storage().referenceForURL("gs://storageURL")
storageRef.child(filePath).putData(data, metadata: metaData){(metaData,error) in
if let error = error {
print(error.localizedDescription)
return
}
if let ProfileImageUrl = metaData?.downloadURL()?.absoluteString {
let values : [String : AnyObject] = ["name": name!, "email": email!, "profileImageUrl": ProfileImageUrl]
self.userPost(values)
}
}
})
}
//save data in Database
func userPost(values: [String: AnyObject]) {
let ref = FIRDatabase.database().referenceFromURL("https://databaseURL.firebaseio.com/")
ref.child("users").childByAutoId().setValue(values)
}
so i got that so far but cant figure out how to download it to user profile.
here is my code for user ProfileVC:
#IBOutlet weak var ProfileImg: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.retriveData()
ProfileImg.image = UIImage(named: "ic_account_circle.png")
imagePicker.delegate = self
}
func DownloadProfilePhoto() {
let storageRef = FIRStorage.storage().referenceForURL("gs://StorageURL.com")
let filePath = "\(FIRAuth.auth()!.currentUser!.uid)/\("userPhoto")"
}
Please Help.....
You need to download the image data, and then create an image. You should try this:
let storage = FIRStorage.storage()
var reference: FIRStorageReference!
reference = self.storage.referenceForURL("gs://appname.appspot.com/filePath")
reference.downloadURLWithCompletion { (url, error) in
let data = NSData(contentsOfURL: url!)
let image = UIImage(data: data!)
ProfileImg.image = image
}
You should check the Firebase documentation https://firebase.google.com/docs/storage/ios/download-files
For people looking for swift 4
let storage = Storage.storage()
var reference: StorageReference!
reference = storage.reference(forURL: "gs://appname.appspot.com/filePath")
reference.downloadURL { (url, error) in
let data = NSData(contentsOf: url!)
let image = UIImage(data: data! as Data)
cell.imgOutlet.image = image
}

Load user image

I use this code to load users usernames when a user search for another user
var user: PFUser? {
didSet {
userNameLabel.text = user?.username
}
}
How can I do the same thing but for profile pictures?
Here is what you can do.
case 1
If you need to download the image and display then -
Create a property that holds url for the image.
Asynchronously download image and display
So code would look this this
var pictureURL: NSURL?
var user: PFUser? {
didSet {
userNameLabel.text = user?.username
pictureURL = user?.pictureURL
displayPicture()
}
}
private func displayPicture() {
// Async download pic and display
}
case 2
If you have the image locally then just display -
var user: PFUser? {
didSet {
userNameLabel.text = user?.username
userImage.image = UIImage(named: "Picturename")
}
}
You can not save your image file with UIImage format on Parse.com. You can store the file with PFFile and you have to convert your image to PFFile for saving and getting the image.
You can save your Image with this function;
private func saveLogo() {
let yourLogoImageFile = UIImage(named: "YourLogoFileName")
if let imageData = UIImageJPEGRepresentation(yourLogoImageFile!, 0.1) {
// You got the PFFile you can use this for save.
let imagePFFile = PFFile(name: "logo.png", data: imageData)
logo = imagePFFile // Send this PFFile to your variable or just save.
// Saving...
imagePFFile?.saveInBackground()
}
}
After that you can get your logo PFFile from Parse and you have to convert it to UIImage with this function;
private func getLogo() {
if let image = yourLogoVariable {
image.getDataInBackgroundWithBlock() {
(let imageData, error) in
if error == nil {
if let logoImageData = imageData {
if let logoImage = UIImage(data: logoImageData) {
// logoImage is the UIImage you can use this for your UIImageView.
}
}
}
}
}
}