Saving an array of images to parse [duplicate] - ios

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

Related

How to upload images to azure without losing quality with image data swift

Currently, I am using below to upload an image in Azure blob
let im:UIImage = catImage[i]
let imageData = im.pngData()
blob.upload(from: imageData!, completionHandler:{(NSError) -> Void in
print( i , "uploaded")
print(blob.metadata)
print(i , imageData)
})
but I am losing image quality because of pngdata().
What else I can use without losing quality.
Similar question here: UIImage to raw NSData / avoid compression
Trying to save a UIImage without compression.
It was a problem with my image picker, solved it by picking phasset instead of images directly

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?

Image Array Causing memory Issue

Currently creating an basic image slideshow application, To do this i have created an UIImage array and calling them with the following code.
let imageNames = (0...50).map
{
"\($0).JPG"
}
let image = UIImage(named: imageNames[0])
imageView.animationImages = image
imageView.animationDuration = 50
imageView.startAnimating()
Was wondering if anyone would be able to offer some advice.
The issue isn't how much memory the image takes on disk, it's about the memory required to address all the pixels on your screen. You don't really need to store the images before you use them. One image of less than 1MB will load very quickly.
Instead you just need to keep a path to the image, and only when you get to the code that displays the image, then you load it, and display it.
So, combining Alexander's and KKRocks on-point answers:
In the top of your class, define your array of filenames:
let imageNames = (0...55).map { "\($0).JPG" }
Where you are displaying your image:
if let image = UIImage(named: images[0]) {
... the code that is blowing up ...
}
If you are not sure how to save a file and retrieve it, please see this answer I recently gave on that subject:
Saving and Retrieving Files in User's Space.
You need to add image name in array instead of UIImage .
let images: [String] = [
"0.JPG",
"1.JPG")!]
Get image from string of array
let image = UIImage(named: images[0])

Saving & retrieving images on Firebase [duplicate]

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.

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