使NSManagedObject可编码并存储图像数据 [英] Making NSManagedObject Codable and Store Image Data

查看:135
本文介绍了使NSManagedObject可编码并存储图像数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发具有离线功能的应用程序.我必须从网络中获取JSON数据并将其存储在Core Data中.其余数据很简单,但是我想知道什么是处理图像的最佳方法. JSON响应提供了URL,然后我必须下载图像并将图像数据存储到Core Data中.我正在制作NSManagedObject Codable,因此可以捕获该URL.我有额外的可选变量 albumImage包含数据.这是正确的方法吗?我将使用albumArtwork来存储URL,我将使用该URL下载图像并将数据存储到albumImage中.此模型中albumImage的唯一目的是存储图像数据,而不处理JSON响应.我只想知道是否有更好的方法来处理这个问题.

I am working on an App with offline capabilities. I have to fetch the JSON data from the network and store it in Core Data. Rest of the data is pretty straight forward, but I am wondering what's the best way to handle the image. JSON response gives the URL and then I have to download the image and store the data of the image into Core Data. I am making NSManagedObject Codable, so I can capture that URL. I am having the extra optional variable albumImage with data. Is this the right approach? I would use albumArtwork to store the URL, I would use that URL to download the image and store the data into albumImage. The sole purpose of albumImage being in this model is to store image data and not handle the JSON response. I just want to know if there is any better way of handling this.

JSON:

{
                "artistName": "Ozuna",
                "id": "1433976640",
                "releaseDate": "2018-08-24",
                "name": "Aura",
                "kind": "album",
                "copyright": "℗ 2018 VP Records Corp./Dimelo Vi Dist. by Sony Music Entertainment U.S. Latin LLC",
                "artistId": "283578837",
                "artistUrl": "https://itunes.apple.com/us/artist/ozuna/283578837?app=music",
                "artworkUrl100": "https://is2-ssl.mzstatic.com/image/thumb/Music128/v4/d7/60/f7/d760f7bf-e2bc-e48e-afcd-b9b562ad4c2f/697691884080.jpg/200x200bb.png"
}

用于处理JSON响应的模型:

public class Album: NSManagedObject, Codable {
    @NSManaged public var artistName: String
    @NSManaged public var albumName: String
    @NSManaged public var copyrightInfo: String
    @NSManaged public var albumArtwork: String
    @NSManaged public var albumReleaseDate: Date
    @NSManaged public var linkToAppStore: String
    @NSManaged public var albumImage: NSData?
}

推荐答案

在CoreData中存储数据blob与将磁盘中的数据blob作为独立文件存储是有优缺点的.存储在文件中意味着您必须跟踪它们并在删除或更改它们时正确清理它们,这有点麻烦.

There are pros and cons with storing data blobs in CoreData vs on the disk as seperate files. Storing in files means you have to keep track of them and clean up properly when they are deleted or changed which is a bit of a pain.

在您的情况下,我只将数据直接存储在CoreData中,但是使用单独的CoreData对象(例如AlbumImage),该对象与Album对象具有一对一的关系.当您仅使用相册"对象并且不希望将图像数据加载到内存中时,这可以使事情保持快速.

In your case I would just store the data directly in CoreData, however use a seperate CoreData object (eg AlbumImage), that has a 1-to-1 relationship with the Album object. This keep things fast when you are just working with Album objects and don't want the image data loaded into memory.

CoreData还具有允许外部存储"选项,该选项实际上无论如何都会将数据存储为磁盘上的文件(一旦满足一定的大小阈值),因此您也可以尝试.

CoreData also has the 'allows external storage' option which will actually store the data as a file on disk anyway (once a certain size threshold is met) so you can try that too.

-

关于您的评论.

如果您有一个AlbumVC列出相册,您可以在其中排序/搜索/过滤,则不必要使用大量内存将所有全尺寸图像数据存储在内存中.如果您有次要对象,则仅在用户深入到AlbumDetailVC时才需要将其加载到内存中.

If you have a AlbumVC listing Albums where you can sort/search/filter then it would be unnecessarily using a lot of ram to have all the full sized image data sitting in memory. If you have a secondary object then it only needs to load into memory when the user drills down into AlbumDetailVC.

您仍然可以保留相同的接口来访问图像,只是让该方法调用子对象.像这样的东西.

You can still keep the same interface to access the image, just have that method call the child object. Something like this.

- (UIImage *)image
{
    return [UIImage imageWithData:self.imageData.data];
}

这篇关于使NSManagedObject可编码并存储图像数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆