Ios swift -在文档目录中存储uiimage,在核心数据中存储路径

时间:2022-05-11 16:59:29

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.

我被建议将图像存储在文档目录中,并将路径保存在core-data中,这样就不会将实际的图像存储在那里,而是简单地存储到可以找到它的地址。

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

有人能把我推向正确的方向吗?似乎找不到合适的指导?

3 个解决方案

#1


1  

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

我能够在这个youtube视频中找到我所有问题的答案——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

这是github上的文件,上面有关于如何完成这个任务的说明——https://github.com/TDAbboud/WeightLogger-Images

#2


0  

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.

试试Hanake cache: https://github.com/Haneke/Haneke对于我个人来说,同样的问题也很有效。这让一个有时痛苦的任务变得相当容易。

#3


0  

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

在文档目录中保存图像很容易,这段代码没有优化只是为了给您一个提示(您应该管理错误、选项等等)。假设您已经有一个名为image的有效UIImage实例和一个名为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
                    }

#1


1  

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

我能够在这个youtube视频中找到我所有问题的答案——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

这是github上的文件,上面有关于如何完成这个任务的说明——https://github.com/TDAbboud/WeightLogger-Images

#2


0  

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.

试试Hanake cache: https://github.com/Haneke/Haneke对于我个人来说,同样的问题也很有效。这让一个有时痛苦的任务变得相当容易。

#3


0  

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

在文档目录中保存图像很容易,这段代码没有优化只是为了给您一个提示(您应该管理错误、选项等等)。假设您已经有一个名为image的有效UIImage实例和一个名为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
                    }