sqlite数据库插入和读取图片数据 (for ios)
生活随笔
收集整理的這篇文章主要介紹了
sqlite数据库插入和读取图片数据 (for ios)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
為什么80%的碼農(nóng)都做不了架構(gòu)師?>>> ??
在iOS下用sqlite數(shù)據(jù)庫存儲圖片,先把你的圖片轉(zhuǎn)換成 NSData 形式,然后在數(shù)據(jù)庫添加一行 blob 數(shù)據(jù)
假定數(shù)據(jù)庫中存在表 test_table(name,image), 下面代碼將圖片文件test.png的二進(jìn)制數(shù)據(jù)寫到sqlite數(shù)據(jù)庫:
char *name = "test"; NSString * nameString = [NSString stringWithCString:name encoding:NSUTF8StringEncoding]; NSString * filePath = [[NSBundle mainBundle] pathForResource:nameString ofType:@"png"]; if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {NSData * imgData = UIImagePNGRepresentation([UIImage imageWithContentsOfFile:filePath]);const char * sequel = "insert into test_table(name,image) values(?,?)";sqlite3_stmt * update;if (sqlite3_prepare_v2(database, sequel, -1, &update, NULL) == SQLITE_OK){sqlite3_bind_text(update, 1, name, -1, NULL);sqlite3_bind_blob(update, 2, [imgData bytes], [imgData length], NULL);if( sqlite3_step(update) == SQLITE_DONE){NSLog(@"已經(jīng)寫入數(shù)據(jù)");}sqlite3_finalize(update);} } else {NSLog(@"文件不存在"); }下面代碼從數(shù)據(jù)庫中讀取圖片二進(jìn)制數(shù)據(jù),然后顯示圖片: const char * sequel = "select image from test_table where name=?"; sqlite3_stmt * getimg; if (sqlite3_prepare_v2(database, sequel, -1, &getimg, NULL) == SQLITE_OK) {char *name = "test";sqlite3_bind_text(update, 1, name, -1, NULL);if(sqlite3_step(getimg) == SQLITE_ROW){int bytes = sqlite3_column_bytes(getimg, 0);Byte * value = (Byte*)sqlite3_column_blob(getimg, 1);if (bytes !=0 && value != NULL){NSData * data = [NSData dataWithBytes:value length:bytes];UIImage * img = [UIImage imageWithData:data];UIImageView * aview =[[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, IMAGE_WIDTH, IMAGE_HEIGHT)];aview.image = img;[self.view addSubview:aview];[aview release];}}sqlite3_finalize(getimg); }
例2:
存儲圖片:
// Save Small Image Data by given main url - (void) SaveImagesToSql: (NSData*) imgData :(NSString*) mainUrl { NSLog( @"\n*****Save image to SQLite*****\n" ); const char* sqliteQuery = "INSERT INTO IMAGES (URL, IMAGE) VALUES (?, ?)"; sqlite3_stmt* statement; if( sqlite3_prepare_v2(articlesDB, sqliteQuery, -1, &statement, NULL) == SQLITE_OK ) { sqlite3_bind_text(statement, 1, [mainUrl UTF8String], -1, SQLITE_TRANSIENT); sqlite3_bind_blob(statement, 2, [imgData bytes], [imgData length], SQLITE_TRANSIENT); sqlite3_step(statement); } else NSLog( @"SaveBody: Failed from sqlite3_prepare_v2. Error is: %s", sqlite3_errmsg(articlesDB) ); // Finalize and close database. sqlite3_finalize(statement); } 讀取圖片:
// Load images from data base with given image url - (NSData*) LoadImagesFromSql: (NSString*) imageLink { NSData* data = nil; NSString* sqliteQuery = [NSString stringWithFormat:@"SELECT IMAGE FROM IMAGES WHERE URL = '%@'", imageLink]; sqlite3_stmt* statement; if( sqlite3_prepare_v2(articlesDB, [sqliteQuery UTF8String], -1, &statement, NULL) == SQLITE_OK ) { if( sqlite3_step(statement) == SQLITE_ROW ) { int length = sqlite3_column_bytes(statement, 0); data = [NSData dataWithBytes:sqlite3_column_blob(statement, 0) length:length]; } } // Finalize and close database. sqlite3_finalize(statement); return data; }
轉(zhuǎn)載于:https://my.oschina.net/amoyai/blog/96451
總結(jié)
以上是生活随笔為你收集整理的sqlite数据库插入和读取图片数据 (for ios)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: OD使用教程16 - 调试篇16
- 下一篇: BACKUP PENDING状态的解除