I try to store array of UIImage, it used to work but somehow all of sudden it refused to work. I wrote this test example to check if it's stored properly and I guess the problem is somewhere here
let images = NSKeyedArchiver.archivedDataWithRootObject(self.globalImageArray)
NSUserDefaults.standardUserDefaults().setObject(images, forKey: "morningImages")
NSUserDefaults.standardUserDefaults().synchronize()
println("Images saved")
let images2 = NSUserDefaults.standardUserDefaults().objectForKey("morningImages") as? NSData
let imagesArray = NSKeyedUnarchiver.unarchiveObjectWithData(images2!) as! NSArray
var testArray = imagesArray as! [UIImage]
println("Check if images are loaded " + "\(testArray.count)")
The testArray.count is equal to zero which means it either fails to save them properly or fails to retrieve them.
I tried printing images2 and it does contain data, but printing the next value which is imagesArray leads to the result equals to "()" Guess the problem is with this line:
let imagesArray = NSKeyedUnarchiver.unarchiveObjectWithData(images2!) as! NSArray
Thanks for help in advance.
As others have pointed out, this really isn't the best use case for NSUserDefaults. This should be very simple to do by writing to and reading from files on disk.
Here's how I'd do it, and it's less code than saving to NSUserDefaults!
NSKeyedArchiver.archiveRootObject(self.globalImageArray, toFile: "/path/to/archive")
println("Images saved")
if let testArray = NSKeyedUnarchiver.unarchiveObjectWithFile("/path/to/archive") as? [UIImage] {
println("Check if images are loaded " + "\(testArray.count)")
} else {
println("Failed to load images.")
}
EDIT As it turns out this doesn't work on cached images (e.g. any UIImage loaded with either of the UIImage(named:) variants because the cached images don't seem to get serialized to disk. So, while the above works for not cached UIImages, the following works regardless of the image's cached status.
// Save the raw data of each image
if NSKeyedArchiver.archiveRootObject(imageArray.map { UIImagePNGRepresentation($0) }, toFile: archivePath) {
println("\(imageArray.count) Images saved")
} else {
println("failed to save images")
}
// Load the raw data
if let dataArray = NSKeyedUnarchiver.unarchiveObjectWithFile(archivePath) as? [NSData] {
// Transform the data items to UIImage items
let testArray = dataArray.map { UIImage(data: $0)! }
println("\(testArray.count) images loaded.")
} else {
println("Failed to load images.")
}
Related
I am uploading an array of images to firebase, which was previously filled by up to three photos taken by camera.
After each upload, I save the downloadURL.
But I see, that the order of the images is random.
I suspect that it depends on the photosize, which photo is uploaded first.
How can I ensure, that the first image in imageArray will be also the first image uploaded and therefore the first downloadURL I get?
func storeInFirestore(var1:String, var2:String, var3:String, var4:String, var5: String) {
guard let user = Auth.auth().currentUser?.uid else {return}
var data = NSData()
var i = 0
for items in imageArray {
i += 1
if i <= imageArray.count {
data = items.images.jpegData(compressionQuality: 0.8)! as NSData
let filePath = "\(user)/images"
let metaData = StorageMetadata()
let ref = Storage.storage().reference().child("\(user)_\(var1)_\(i)")
metaData.contentType = "image/jpg"
ref.putData(data as Data, metadata: metaData){(metaData,error) in
if let error = error {
print(error.localizedDescription)
return
}else{
[get the downloadURL and store in array...]
Just enumerate the loop and use n, the index value, to construct the array. You can also use a dictionary instead of an array and simply use n as the key (and the file name as the value).
for (n, img) in imageArray.enumerated() {
let data = img.images.jpegData(compressionQuality: 0.8)
let filePath = "\(user)/images"
let storageRef = Storage.storage().reference().child("\(user)_\(var1)_\(n)")
let metaData = StorageMetadata()
metaData.contentType = "image/jpg"
storageRef.putData(data, metadata: metaData) { (metaData, error) in
if let _ = metaData {
// image successfully saved to storage
// Remember, `n` is still in scope which is the array index
// (i.e. 0 is the first image, 1 is the second, etc.) so
// simply construct your array using these indices. To simplify
// things, you can use a dictionary here instead of an array,
// which could look something like `remoteImagePaths[n] = remotePath`.
} else {
if let error = error {
print(error.localizedDescription)
}
}
}
}
Solution
Well kind of... knowing if an uploaded image is the first in the list when the callback is triggered can be tricky as the first image could be massive, so it takes a while to upload, then the second image is small, so it doesn't take as long, therefore the second image is uploaded first. I gather you already know this from your post though, just wanted to clarify this for others who visit this issue.
Now as for fixing it, there's a few ways, but I think the cleanest one is this.
func storeInFirestore(var1:String, var2:String, var3:String, var4:String, var5: String, completion: #escaping([URL]) -> Void) {
guard let user = Auth.auth().currentUser?.uid else {return}
var data = NSData()
var imageToUrlsPair = [UIImage: URL]()
var imagesDownloadedCount: Int = 0
for items in imageArray {
let data = items.images.jpegData(compressionQuality: 0.8)
let filePath = "\(user)/images"
let metaData = StorageMetadata()
let ref = Storage.storage().reference().child("\(user)_\(var1)_\(i)")
metaData.contentType = "image/jpg"
ref.putData(data, metadata: metaData){(metaData,error) in
if let error = error {
imagesDownloadedCount += 1 // Handle this error however you please, you could fail this entire request.
print(error.localizedDescription)
return
} else {
imagesDownloadedCount += 1
imageToUrlsPair[items.images] = [get the downloadURL and store in array...]
if imagesDownloadedCount == imageArray.count {
completion(imageToUrlsPair.map { $0.value })
}
}
}
}
}
So to sum up what I've done:
I've made a dictionary to contain a pair of images to urls (the imageToUrlsPair variables)
Once an image is downloaded, it adds the url to the associated image and increments the downloaded image counter (imagesDownloadedCount)
Once the final image is downloaded, the imagesDownloadedCount will equal the imageArray.count so it will trigger the completion callback.
I have added a completion callback so that this function performs its network requests asynchronously and returns the urls once all requests have been completed in the background.
I define var dictImages = NSMutableDictionary() to store downloaded images and to not load more than one time, but when I remove row from tableView in Firebase, images are sliding. I figured out that problem is in dictImages. The only way to prevent that error is to write
dictImages.removeAllObjects()
before downloading images, but in this case there is no meaning of storing images and it is not efficient. I need to figure out previously stored item and remove it.
This code is written in cellForRowAt function of tableView :
if let image = self.dictImages.object(forKey: indexPath)
{
cell?.newsImg.image = image as? UIImage
}
else{
storageRef.getData(maxSize: 1 * 1024 * 1024) { data, error in
if let error = error {
print(error)
} else {
if let data = data
{
let image = UIImage(data: data)
self.dictImages.setObject(image, forKey: indexPath as NSCopying)
cell?.newsImg.image = image
}
}
}
Firebase Storage Image Not Downloading in Tableview. If I replace the line let tempImageRef = storage.child("myFiles/sample.jpg"), it's showing the single image. But if try to grab all the images inside 'myFiles' , it doesn't work. Please help
func fetchPhotos()
{
//let database = FIRDatabase.database().reference()
let storage = FIRStorage.storage().reference(forURL: "gs://fir-test-bafca.appspot.com")
let tempImageRef = storage.child("myFiles/")
tempImageRef.data(withMaxSize: (1*1024*1024)) { (data, error) in
if error != nil{
print(error!)
}else{
print(data!)
let pic = UIImage(data: data!)
self.picArray.append(pic!)
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
}
}
You are referencing an entire folder (myFiles/). That wont work. You need to reference each image individually.
One way to do this is to write the image metadata to your realtime database when you upload an image. Then read the metadata (more specifically the path) and then reference the image that way.
If you have 3 images you want to upload and store, you could write the metadata as follows:
/images
/image1key
/metdata
/path
/image2key
/metdata
/path
/image3key
/metdata
/path
Then you can query your database for your images path's like so
let ref = Firebase.database().ref("images")
ref.observe(.value, withCompletion: { snapshot in
if let values = snapshot.value as? [String : Any] {
if let metadata = values["metadata"] as? [String: Any] {
let imagePath = metadata["path"]
//Now download your image from storage.
}
}
})
This isn't the cleanest code and you can definitely improve, but it will work :)
I'm working on a Cocktailapp and want to save my image data to Core Data.
I watched some tutorials and did the same but it's still not working an I don't know why.
I have an Array with all titles for the images:
let imagesAperitif: [String] = ["americano.jpg", "daiquiri.jpg",
"frozen daiquiri.jpg",
"banana frozen daiquiri.jpg", "bronx.jpg", "kir.jpg",
"hugo.jpg", "Manhattann.jpg", "manhattan dry.jpg", "manhattan medium.jpg", "margarita.jpg",
"martini dry.jpg",...
Thats where I call my method for saving the images to Core Data:
setCocktails(nameInsert, zutaten: zutatenInsert, zubereitung: zubereitungInsert, dekoration: dekorationInsert, stil: stilInsert, bild: UIImage(named: imagesAperitif[index])!)
That's a part from the code of saveCocktails method:
let imageData = NSData(data: UIImageJPEGRepresentation(bild, 1.0)!)
eintrag.setValue(imageData, forKey: "bild")
do {
try managedContext.save()
That's part of the fetching method:
let fetchRequest = NSFetchRequest(entityName: "Cocktail")
do {
let results =
try managedContext.executeFetchRequest(fetchRequest)
cocktails = results as! [NSManagedObject]
And here I want to get my image back from NSData:
imagesAperitif.append(UIImage(data: eintrag.valueForKey("bild") as! NSData)!)
But the App crashes with this line and I get a "fatal error: unexpectedly found nil while unwrapping an Optional value" error.
Is anybody able to explain this to me because I don't know what to change. Everything I tried went also wrong.
You may want to check nil for eintrag.valueForKey("bild") before case it to NSData
As
func valueForKey(_ key: String) -> AnyObject?
And it's always save to check nil before you append UIImage(data: eintrag.valueForKey("bild") as! NSData)
(I like using guard so here goes :) )
So what I would do here since it looks like the inserting of the NSData object fails:
// We are simultaniously unwrapping objects and checking if objects are nil.
guard let imgAsNSData: NSData = UIImageJPEGRepresentation(bild, 1.0),
let entity: Cocktail = eintrag as? Cocktail else {
// Stop executing the method here, there is no point in going further. Handle any errors here! Either imgAsNSData is nil, or could not cast to your class. A guard statement handles it's errors here.
return
}
// At this point we know we have an NSData object. Assign it to the entity.
entity.bild = imgAsNSData
do {
// Save our entity.
try managedContext.save()
} catch {
// Handle error
}
Fetching:
let fetchRequest = NSFetchRequest(entityName: "Cocktail")
do {
let results: [NSManagedObject] = try managedContext.executeFetchRequest(fetchRequest)
guard let cocktails = results as? [Cocktail] else {
// Could not cast to an array of Cocktail objects.
return
}
// Do stuff with the cocktails object.
Add to your array:
// 1: Check if entity not is nil
// 2: Check if entity's bild property not is nil.
// 3: Check if we can create an image using the NSData
guard let cocktail: Cocktail = eintrag as? Cocktail,
let imgAsNSData: NSData = cocktail.bild,
let image: UIImage = UIImage(data: imgAsNSData) else {
// Required values are nil. Cannot proceed.
return
}
imagesAperitif.append(image)
Untested code so be careful :)
My question: what is the best way to know when the last image has been downloaded, since once all files have been downloaded I need to update the UI.
I am downloading an array of PFFiles from Parse. What I am doing the moment is: getting the array of PFFiles from parse this way
func getAllUserPictures(completion: (imageFiles: [PFFile]) -> Void) {
guard let id = currentUserID else {
return
}
let query = PFQuery(className: "Profile")
query.whereKey("objectId", equalTo: id)
query.includeKey("avatars")
query.findObjectsInBackgroundWithBlock { profile, error in
guard let imageFiles = profile?.first?.objectForKey("avatars") as? [PFFile] else {
return
}
completion(imageFiles: imageFiles)
}
}
After having obtained the imageFile array from the completion handler I do proceed to download them this way:
func getUserPicturesWithBlock(completion: (result: [UIImage]) -> Void) {
DataManager.sharedInstance.getAllUserPictures { imageFiles in
for file in imageFiles {
file.getDataInBackgroundWithBlock({ data, error in
guard let data = data, let image = UIImage (data: data) else{
return
}
self.imagesArray.append(image)
})
}
}
}
Have you considered using multiple PFImageView from the ParseUI framework?
You could assign each of the PFFiles to a PFImageView in the completion of the query and then call loadInBackground on each PFImageView. The PFImageView class automatically handles updating the images once the data has been loaded and this would also update each image as soon as it's fetched rather than waiting for all of them together.
Alternatively, to keep a similar structure to what you already have, you could use the size of the imageFiles array to tell when they have all been loaded. In the completion of loading each file, you could check if the number of loaded images is equal to the total size of the array.