首先,对不起。 我不擅长英语。 你好 我是iOS的初学者。 目前,我正在这样开发: 删除自动生成的核心数据sqlite文件 使用firefox sqlite管理器将我创建的sqlite文件复制到已删除的sqlite文件路径 (使用Z_PK,Z_ENT,Z_OPT ...
提示:本站收集StackOverFlow近2千万问答,支持中英文搜索,鼠标放在语句上弹窗显示对应的参考中文或英文, 本站还提供 中文繁体 英文版本 中英对照 版本,有任何建议请联系yoyou2525@163.com。
我已经通过先通过核心数据创建数据库,然后使用SQLite Manager填充该初始化文件来为我的应用程序预先填充数据。 是否可以预填充SQLite表中的图像以用于核心数据?
我最初的想法是通过SQLite Manager将图像作为Blob插入。 然后根据这篇文章 ,看来我需要将类型设置为二进制并使用UIImage initWithData:
导入。
这可行吗?如果可以,这是否合适?
将图像预填充到SQLite数据库中以与Core Data一起使用非常简单。
- (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;
}
导航到“用户//库/应用程序支持/ iPhone模拟器/用户/应用程序/”中的应用程序数据。 如果按“修改日期”对文件夹进行排序,则您的应用程序将具有最新日期(假设您同时没有构建任何其他应用程序)。 输入应用程序文件夹,初始化的<YourDBName.sqlite>将驻留在Documents文件夹中。 将SQLite数据库复制到另一个位置(例如您的桌面)并删除原始文件(这是允许Core Data重新加载要创建的预填充SQLite数据库所必需的)。
用您喜欢的SQLite编辑器打开<YourDBName.sqlite>(适用于Firefox的SQLite Manager插件是一个适当的免费选项)。 在表中添加条目,将所有图像插入为“ BLOB”。
在XCode中,添加<YourDBName.sqlite>作为现有文件。 如果下一次启动应用程序,Core Data会将其复制到应用程序数据文件夹中(如果该文件尚不存在)(您删除了原始权限吗?)。
使用[UIImage imageWithData:< DataObject >.< ImageAttributeName >
在代码中访问预先填充的图像[UIImage imageWithData:< DataObject >.< ImageAttributeName >
您的图片有多大? 如果它们很大,则可以通过将映像存储在文件系统中并在核心数据中保留对其位置的引用来更好地服务。
如果图像将始终存在于您的应用程序中,则可以将其与捆绑包打包在一起。 如果不是这样(例如,用户可以删除不需要的图像),则可能必须在首次使用时依靠拉入图像。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.