[英]Want advise on saving images in sqlite3 on ios
我正在為iOS開發一個使用SQLite3數據庫的應用程序,現在我想在其中保存一些圖像,我在網上搜索到人們告訴我,像這樣將圖像保存在SQLite3中並不是一個好主意。
我完全感到困惑,該怎么辦。我正在將我的整個情況描繪在你面前,請告訴我該怎么做
我想在我的SQLite數據庫中存儲79個圖像,大多數圖像大小為2kb,很少有20至25kb,所有圖像總共需要384kb磁盤空間,因此建議將所有圖像存儲在數據庫中或僅使用鏈接我的圖像數據庫和文件系統
請盡快告訴我
好吧,我不會建議將圖像存儲在數據庫中。 主要原因是可以導致數據庫崩潰(我不會總是說,但是,在某些情況下,我確實看到由於存儲圖像而導致數據庫崩潰)。
緩存圖像的最好方法是將圖像保存到文檔目錄中。 這是安全可靠的。 編碼愉快。 :-)
我認為將imagePath保存在SQLite中,並將圖像保存在文檔目錄中。 我也一樣。 可能對您有幫助。
-(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]);
}
我同意將大量Blob存儲在數據庫中會導致網站和其他擁有大量用戶的應用程序出現性能問題。 數據庫資源浪費在可能由文件系統或其他服務器處理的長時間操作上。
但是,使用iPhone應用程序,您無需擔心數據庫資源浪費在Blob上。 您的應用程序只有1個用戶訪問數據庫,因此,如果圖像來自文件系統或SQLite,它將具有同樣的響應速度。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.