2012-10-31 49 views
1

我正在开发一款应用程序,它使用SQLite3数据库,现在我想保存一些图像,我在网上搜索过,人们告诉说,保存图像不是个好主意在sqlite3的这样一个想要建议在ios上保存图像到sqlite3上

Blob Data Type?

我完全混淆了,这样做我画我在你面前的整个情况是什么请指点我该怎么办

我要存储79个图像我的SQLite数据库大多数都是2kb的大小,很少有20到25kb的磁盘上所有的镜像都需要384kb是它最好在我的数据库或在我的数据库只有使用链接和文件系统的所有图像存储图像

请尽快

+0

如果图像的数量是静态的,那么你可以自由地存储任何地方......但如果插入的数据是大/二进制/对象,那么它将存储在文档目录中是正确的方式。 –

回答

0

我认为在SQLite中保存imagePath,并将图像保存到文档目录中。我也一样。可能是这个帮助你。

-(void)saveDataToDatabase:(NSString *)fileNameToSave 
{ 

NSString *docsDir; 
NSArray *dirPaths; 
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 

docsDir = [dirPaths objectAtIndex:0]; 
sqlite3_stmt *statement; 
NSString *databasePath = [[NSString alloc] 
          initWithString: [docsDir stringByAppendingPathComponent: 
              @"scanner.db"]]; 

NSLog(@"%@",databasePath); 

const char *dbpath = [databasePath UTF8String]; 

if (sqlite3_open(dbpath, &databaseHandle) == SQLITE_OK) 
{ 
    NSString *insertSQL = [NSString stringWithFormat: @"Insert into IMAGEINFO (NAME,IMAGEPATH) values ('%@','%@')",@"imageFirst",fileNameToSave]; 
    const char *insert_stmt = [insertSQL UTF8String]; 
    sqlite3_prepare_v2(databaseHandle, insert_stmt, -1, &statement, NULL); 
    if (sqlite3_step(statement) == SQLITE_DONE) 
    { 
     [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"database"]; 
    } 
    else 
    { 
    } 
} 

} 


-(void)saveImage:(NSString *)fileName:(UIImage *)imageToSave 
{ 
NSError *error; 
NSString *fileNaToSave = [NSString stringWithFormat:@"Documents/%@.png",fileName]; 
NSString *pngPath = [NSHomeDirectory() stringByAppendingPathComponent:fileNaToSave]; 

// Write image to PNG 
[UIImagePNGRepresentation(imageToSave) writeToFile:pngPath atomically:YES]; 
// Let's check to see if files were successfully written... 
// You can try this when debugging on-device 

// Create file manager 
NSFileManager *fileMgr = [NSFileManager defaultManager]; 

// Point to Document directory 
NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]; 

// Write out the contents of home directory to console 
NSLog(@"Documents directory: %@", [fileMgr contentsOfDirectoryAtPath:documentsDirectory error:&error]); 

} 
0

那么告诉我,我不会推荐存储在数据库中的图像。主要原因是可能导致你的数据库崩溃(我不会总是说,但有些情况下,我真的看到了数据库崩溃,由于存储图像)。

缓存图像的最佳方式是将图像保存到文档目录中。这是安全可靠的。快乐编码。 :-)

+0

虽然我不怀疑这里有意义的是将图像保存在数据库旁边,但不是在里面,如果你说的是真的,你需要用SQLite提交一个带有repro的bug报告,因为这是一个非常严重的指控,对于每天在各种设备中将二进制数据存储在SQLite中的亿万SQLite用户而言,这是非常大的影响。 – tomfanning

+0

我很抱歉。我不知道数以亿计的SQLite用户,但我亲自遇到过这种情况。然后,我只用来将图像保存为Documents目录中的文件。你也可以参考这个... http://stackoverflow.com/questions/5291724/storing-images-in-file-system-vs-core-data –

+0

我不怀疑你确实遇到过这种情况 - 但如果事实上,您可以根据需要重现此行为,提交错误报告非常重要。 – tomfanning

0

我同意在数据库中存储较大的斑点会导致网站和其他拥有大量用户的应用程序出现性能问题。数据库资源浪费在可由文件系统或其他服务器处理的长操作上。

但是,使用iPhone应用程序,您不必担心数据库资源被浪费在斑点上。你的应用程序只有1个用户访问数据库,所以如果图像来自文件系统或SQLite,它就会有同样的响应。