1

I have one UiTableView , I need to have one Array Data Saving UIimage for Temporary time .

I Think if I use UserDefault it's not bad solution but I don't know how save in UserDefault I know I don't use .stringArray I don't know how can I Save Image in Array Default

struct Constants {

    static let myKeyImage = "myKeyIMG”

}


 var arrayiMG = [String]()
  self.defaults.stringArray(forKey: Constants.myKeyImage)


if (self.defaults.stringArray(forKey: Constants.myKeyImage) != nil)
{
arrayUrl = self.defaults.stringArray(forKey: Constants.myKeyURL)!
}

I want to save in .png or jpg Like this

func plane(from anchor: ARPlaneAnchor) -> SCNNode {
    if (`extension`.lowercased() == "png") {
        data = UIImagePNGRepresentation(image)
    }
    else if (`extension`.lowercased() == "jpg") {
        data = .uiImageJPEGRepresentation()
    }

}
Miloo
  • 117
  • 1
  • 2
  • 10
  • `extension` is really awful name – Vyacheslav Feb 11 '18 at 21:36
  • @Vyacheslav if it's possible please give me one good way and one good array data – Miloo Feb 11 '18 at 21:43
  • *"I Think if I use UserDefault it's not bad solution"* - it is a bad solution. UserDefaults is not meant to store lots data. – rmaddy Feb 11 '18 at 22:41
  • @rmaddy I need to save for Temporary time and after I need to delete the image array do you have better solutions ? – Miloo Feb 11 '18 at 23:37
  • As @Jake stated at the end of his answer, write the images to files. – rmaddy Feb 11 '18 at 23:53
  • @Miloo If it is temporary just save them at a temporary location. https://stackoverflow.com/questions/11897825/ios-temporary-folder-location/39318537?s=1|42.0586#39318537 . Btw what do you mean by temporary? Temporary files should just persist long enough for you to execute a method/function. Another option would be using the caches folder or NSCache to store it in memory. I suggest you to take a look at Apple File System Basics documentation https://developer.apple.com/library/content/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/FileSystemOverview/FileSystemOverview.html – Leo Dabus Feb 12 '18 at 01:39
  • @LeoDabus I can't find one example for NSCache swift in stack overflow if you can give me one sample code or for my question do you have answer thanks – Miloo Feb 12 '18 at 09:01

1 Answers1

4

Something like this would work:

extension UserDefaults {
    func imageArray(forKey key: String) -> [UIImage]? {
        guard let array = self.array(forKey: key) as? [Data] else {
            return nil
        }
        return array.flatMap() { UIImage(data: $0) }
    }

    func set(_ imageArray: [UIImage], forKey key: String) {
        self.set(imageArray.flatMap({ UIImagePNGRepresentation($0) }), forKey: key)
    }
}

Usage:

let imageArray = [image1, image2, image3]
UserDefaults.standard.set(imageArray, forKey: "images")

let images = UserDefaults.standard.imageArray(forKey: "image")

However, I wouldn't recommend storing an array of images in UserDefaults. Storing the images as files would probably be more appropriate. You could then store their paths in UserDefaults.

Jake
  • 13,097
  • 9
  • 44
  • 73