【问题标题】:Core Data: Pre-populate SQLite Entity with Image核心数据:使用图像预填充 SQLite 实体
【发布时间】:2011-09-29 04:08:27
【问题描述】:

我已经为我的应用程序预先填充数据,首先通过核心数据创建数据库,然后使用 SQLite 管理器填充该初始化文件。是否可以在 SQLite 表中预填充图像以用于核心数据?

我最初的想法是通过 SQLite 管理器将图像作为 blob 插入。然后基于这个post,看来我需要将类型设置为二进制并使用UIImage initWithData: 导入。

这是否可行,如果可行,这是合适的方法吗?

【问题讨论】:

    标签: iphone image sqlite core-data populate


    【解决方案1】:

    将图像预填充到 SQLite 数据库以与 Core Data 一起使用非常简单。

    1. 首先配置您的Core Data 应用程序,实现您的属性以将图像包含为“二进制”类型。使用应用程序模拟器中的 Core Data 构建应用程序并导航到您的视图。这将根据需要初始化 SQLite 数据库以与 Core Data 一起使用(假设您已按如下方式实现了 persistentStoreCoordinator)。
    - (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
        if (persistentStoreCoordinator != nil) {
            return persistentStoreCoordinator;
        }
        NSString *storePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"YourDBName.sqlite"];
    
        // Set up the store.
        // For the sake of illustration, provide a pre-populated default store.
    
        NSFileManager *fileManager = [NSFileManager defaultManager];
        // If the expected store doesn’t exist, copy the default store.
        if (![fileManager fileExistsAtPath:storePath]) {
            NSString *defaultStorePath = [[NSBundle mainBundle] pathForResource:@"YourDBName" ofType:@"sqlite"];
            if (defaultStorePath) {
                [fileManager copyItemAtPath:defaultStorePath toPath:storePath error:NULL];
            }
        }
    
        NSURL *storeUrl = [NSURL fileURLWithPath:storePath];
        NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
                                 [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, 
                                 [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, 
                                 nil];
        persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: [self managedObjectModel]];
    
        NSError *error;
        if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:options error:&error]) {
            // Update to handle the error appropriately.
            NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
            exit(-1);  // Fail
        }
        return persistentStoreCoordinator;
    }
    
    1. 导航到“Users//Library/Application Support/iPhone Simulator/User/Applications/”处的应用程序数据。如果您按“修改日期”对文件夹进行排序,您的应用程序将具有最新日期(假设您同时没有构建任何其他应用程序)。进入应用程序文件夹,初始化的 将驻留在 Documents 文件夹中。将 SQLite 数据库复制到另一个位置(例如您的桌面)并删除原始文件(这是允许 Core Data 重新加载您将要创建的预填充 SQLite 数据库所必需的)。

    2. 使用您最喜欢的 SQLite 编辑器打开 (Firefox 的 SQLite Manager 插件是一个合适的免费选项)。向表中添加条目,将任何图像作为“BLOB”插入。

    3. 在 XCode 中添加 作为现有文件。 Core Data 会在您下次启动应用程序时将此文件复制到应用程序数据文件夹中(如果该文件夹尚不存在)(您删除了原始文件对吗?)。

    4. 使用[UIImage imageWithData:< DataObject >.< ImageAttributeName >访问代码中预填充的图像

    【讨论】:

      【解决方案2】:

      您的图片有多大?如果它们相当大,您可以通过将图像存储在文件系统中并在核心数据中保留对其位置的引用来为您提供更好的服务。

      如果图像将始终存在于您的应用中,那么您可以将它们与您的捆绑包一起打包。如果不是(例如,用户可以删除不需要的图像),您可能不得不依赖于在首次使用时拉取图像。

      【讨论】:

      • 图像将仅为缩略图(大约 8 到 10kB)。如果可能的话,我想将它们放在数据库中,以便可以通过更新数据库一次处理数据更新。
      • 每当我预播数据库时,我都会尝试让模拟器/设备完成工作,这样我就知道它会兼容。我假设用户将能够添加新图像,因此我将使用您创建的任何方法来完成向 Core Data 中的 NSData 字段添加和检索图像的工作,以进行数据库的初始预播。
      • 在这种情况下,最终用户将无法更新数据库,但是,通过应用程序填充肯定会简化获取图像的过程。
      猜你喜欢
      • 2017-01-10
      • 1970-01-01
      • 2011-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-17
      • 2015-07-04
      • 1970-01-01
      相关资源
      最近更新 更多