Walking
Walking

Reputation: 477

Ios swift - storing uiimage in document directory and path in core data

I'm currently storing my images directly in core data and experiencing issues when having to retrieve a high quality images.

I was advised to store the images in the document directory and save a path in core-data as to not store the actual images there but simply the address of where I can go and find it.

Can someone push me in the right direction? Can't seem to find the appropriate guidance?

Upvotes: 2

Views: 1728

Answers (3)

Walking
Walking

Reputation: 477

I was able to find the answers to all my questions in this youtube video - https://www.youtube.com/watch?v=6vk4UrJR8WM

This is the github with the file that has instructions as to how to complete this - https://github.com/TDAbboud/WeightLogger-Images

Upvotes: 1

Andrea
Andrea

Reputation: 26385

Save an image in Document directory is easy this code is not optimized is just for give you a hint( you should manage errors, optionals etc). Suppose that you already have a valid UIImage instance called image and a valid image name String called imageName

                let data = UIImagePNGRepresentation(image)
                // Unwrapping the data optional
                if let data = data {
                    let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
                    let path = (paths.first! as! NSString).stringByAppendingPathComponent(imageName)
                    data.writeToFile(path, options: [])
                  //Save the path into your managed object
                    }

Upvotes: 0

PyPiePi
PyPiePi

Reputation: 253

Try Hanake cache: https://github.com/Haneke/Haneke It worked really well for me personally with the same problem. It makes a sometimes painful task pretty easy.

Upvotes: 0

Related Questions