2012-10-31 30 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,它就會有同樣的響應。