saoudrizwan / Disk

Easily persist structs, images, and data on iOS
MIT License
3.1k stars 170 forks source link

FEATURE REQUEST: UIImage to JSON conversion #35

Closed EthanSK closed 6 years ago

EthanSK commented 6 years ago

It is currently relatively slow to save many images to a folder. Using the new codable protocol, wouldn't it be a lot faster to convert the uiimage to base64, then save it as json, and then convert it back when retrieving it? It would be cool to have this option.

saoudrizwan commented 6 years ago

By "relatively slow" you mean saving many images to the file system is slow, that's correct when you're writing megabytes of data to disk. The best way to deal with a large amount of data in that case is to use a non-main thread (i.e. background) to do the disk operation (see Large Files) and then in your JSON files, store URLs to those images (or just the names of the images or folder name so you can access them with Disk.) If quality loss isn't an impediment, I suggest using UIImageJPEGRepresentation(_:_:) to compress the images enough to make the file write times faster.

It would not be faster to convert the image to base64 manually and then save it in a JSON file since using PNG or JPEG compression is much more performant and efficient because of their compression algorithms. The reason so many different file types exist (i.e. png, jpeg, mov, mp4, etc.) is because each of these types store the 0s and 1s in certain orders to get the smallest possible size and the highest possible quality for their specific data type.