Saving & retrieving images on Firebase [duplicate] - ios

This question already has answers here:
Swift2 retrieving images from Firebase
(2 answers)
Closed 7 years ago.
On the process of trying Firebase, as a possible replacement to Parse.com (unfortunately to disappear), I have saved a PNG image online using the code below, in Swift.
let fn = self.toolBox.getDocumentsDirectory().stringByAppendingPathComponent("M0-1.png")
let im = UIImage(contentsOfFile: fn),
dat = UIImagePNGRepresentation(im!),
b64 = dat?.base64EncodedStringWithOptions(.Encoding64CharacterLineLength),
qs = ["string": b64!],
r = diltRootRef.childByAppendingPath("zs"),
us = ["img": qs]
r.setValue(us)
The saving part seems to work, but how am I suppose to get back the image I saved? All I have tried so far failed.

I would recommend retrieving images using observeSingleEventOfType(:_), because it's a one-time read.
Once you have the string value synchronized, you can use an NSData() initializer, and then create an UIImage.
imageRef.observeSingleEventOfType(.Value) { (imageSnap: FDataSnapshot!) in
let base64String = imageSnap.value as! String
let decodedData = NSData(base64EncodedString: base64String, options: NSDataBase64DecodingOptions(rawValue: 0))
let image = UIImage(data: decodedData!)
}
Check out this example repo on using base64 images in a UITableView.

Related

Unable to set <UIImage:0x283942880 anonymous {1080, 1080} renderingMode=automatic> to UIImageView XCode Swift

I have built an app which fetches contacts from phonebook and saves their name and photo. To save the photo I've used the following code
if let imageData = contact.thumbnailImageData {
imageStr = String(describing: UIImage(data: imageData)!)
} else {
imageStr = "null"
}
and when I print imageStr using print("IMGSTR: \(imageStr)") I get the following output
IMSTR: <UIImage:0x283942880 anonymous {1080, 1080} renderingMode=automatic>
Now I'm stuck on how to set this string to the UIImageView, I tried
imageview.image = UIImage(named: imageStr)
but it shows nothing
Could someone please help me in how to set the string <UIImage:0x283942880 anonymous {1080, 1080} renderingMode=automatic> to UIImageView?
No need to convert it to a String. UserDefaults supports Data objects. Store it as Data and when setting it to a UIImageView use let image = UIImage(data : imageData)
If you want to convert an instance of Data to String, you should use the String(decoding:as:) initializer, like this.(eg : let str = String(decoding: data, as: UTF8.self)).

Saving images on Xcode simulator

I'm trying to save images using their file paths but every time that I kill the Xcode simulator and restart it, the location of the images changes and my imageView is blank. I know for a fact that the location changes because I print out the file path when I try to pick an image from the photo library and it's different each time.
I access the URL of the UIImage
let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage
let url = info[UIImagePickerController.InfoKey.imageURL] as! URL
After which I save the URL as a string to my realm database. I've tried converting it to string via all of these
let imageFilePath = url.relativeString
let imageFilePath = url.absoluteString
let imageFilePath = url.relativePath
but none of them worked. I'm a newbie to Swift so let me know if I've missed any key details needed.

UIImagePNGRepresentation always nil for generated QR image [duplicate]

This question already has answers here:
PNG/JPEG representation from CIImage always returns nil
(1 answer)
CIFilter output image nil
(2 answers)
Closed 4 years ago.
I am trying to save a QR code image that was generated in my app, and when doing so I am unable to get the underlying data so that I can save it.
The QR code is generated via CIFilters from a string, nothing special, like so:
if let filter = CIFilter(name: "CIQRCodeGenerator") {
//set the data to the contact data
filter.setValue(contactData, forKey: "inputMessage")
filter.setValue("L", forKey: "inputCorrectionLevel")
if let qrImage = filter.outputImage {
self.qrCode = UIImage(ciImage: codeImage)
}
}
}
And, I'm trying to get the image data to save it to the application documents by doing so:
let data = UIImagePNGRepresentation(self.qrCode)
But, it always returns nil.
I understand the documentation saying that it can return nil if no underlying data is found,
but I am confused at how I can store this image that is generated if there is no underlying data?

Saving an array of images to parse [duplicate]

This question already has an answer here:
Parse array images saving and fetching
(1 answer)
Closed 5 years ago.
I have an array of images in a UIImage array and I'm looking to save this to parse. I know how to save a regular image to Swift but how do I send multiple images that are all related to each other.
Basically I have a mosaic app that takes an image and beaks it down into 30 smaller images. How can I save those 30 images together to parse with text and other fields as well.
convert image to data
let imageData = UIImagePNGRepresentation(image)
convert data to image
let image = UIImage.init(data: imageData)
Updated
let imagArr = [UIImage.init(named: "a.jpg"),UIImage.init(named: "b.jpg"),UIImage.init(named: "c.jpg")]
let dataArr = NSMutableArray()
for eachImage in imagArr{
dataArr.add(UIImagePNGRepresentation(eachImage!)! as Data)
}
let me know is it helpful or not
compress image if size is big or leave, convert image to to data and data to base64string .. save string to parst ..
on return .. convert base64 string to data and data to image ..
This would be efficient i guess .. i am doing this for iCloud s
synchronization

How do I create an image from a Base64-encoded string in Swift? [duplicate]

This question already has answers here:
Convert between UIImage and Base64 string
(24 answers)
Closed 6 years ago.
A web service echoes a Base64 encoded image as a string. How can one decode and display this encoded image in a Swift project?
Specifically, I would like to take an image, which is already provided by the web service as a string in Base64 format, and understand how to display it in a UIImageView.
The articles I have found thus far describe deprecated techniques or are written in Objective-C, which I am not familiar with. How do you take in a Base64-encoded string and convert it to a UIImage?
Turn your base64 encoded string into an NSData instance by doing something like this:
let encodedImageData = ... get string from your web service ...
let imageData = NSData(base64EncodedString: encodedImageData options: .allZeros)
Then turn the imageData into a UIImage:
let image = UIImage(data: imageData)
You can then set the image on a UIImageView for example:
imageView.image = image
To decode Base64 encoded string to image, you can use the following code in Swift:
let decodedData = NSData(base64EncodedString: base64String, options: NSDataBase64DecodingOptions.fromRaw(0)!)
var decodedimage = UIImage(data: decodedData)
println(decodedimage)
yourImageView.image = decodedimage as UIImage
Even better, you can check if decodedimage is nil or not before assigning to image view.

Resources