0
votes

How can I store a GIF image fetched from HTTP request in Core Data? I already store my still images with UIImagePNGRepresentation as NSData in a Binary data attribute, but I have how to proceed with a gif?

Edit: What I've done so far is getting the data from the request, then storing it in the BinaryData using UIImagePNGRepresentation. But when I try to create a UIImage with SwiftyGif, an error message shows up saying Could not determine delay times for GIF. So I guess the PNG representation mess with the frames of the GIF. I also tried to directly store the data from the http request but then the UIView throw me this error: Terminating app due to uncaught exception 'CALayerInvalidGeometry', reason: 'CALayer position contains NaN

2
There is really no difference. You can dump GIF into binary as well.Rob Zombie

2 Answers

0
votes

A gif is a file format, and files are data and data can be stored in core data. Nevertheless it s a bad practice to store large amount of binary data in any database, and a filesystem is often a better choice. I would recommend storing the files in the file system and keeping relative file path in the database (make sure it is relative, because the app's container directory can change). If these are images that are retrieve from a server and can be refetched they should be stored in the tmp directory (or should just be managed separately with something like SDWebImage). If they are not retrievable later then they should be stored in the documents directory.

If that seems to hard then you can still store them in core-data. Core-data does has an option to allow it to store property as files outside of the database file ("Allows External Storage").

0
votes

When you call UIImagePNGRepresentation, you're converting PNG data to a Swift Data structure. If you save it in Core Data, you'll be using a binary attribute.

You didn't mention how you're storing your GIFs. If you have them in Data instances then you do it exactly as with the PNGs-- with a Core Data binary attribute. If you have something other than a Data, please explain what kind of object or struct you're using to hold the GIF data.