1
我已保存和检索sqlite的图像,我想要做的PDF一样(我曾作为本地文件)...保存和检索的PDF和SQLite的iOS中
- (void)saveImage
{
sqlite3_stmt *compiledStmt;
sqlite3 *db;
if(sqlite3_open([dbPath UTF8String], &db)==SQLITE_OK){
NSString *[email protected]"insert into Image(image) VALUES(?)";
if(sqlite3_prepare_v2(db,[insertSQL cStringUsingEncoding:NSUTF8StringEncoding], -1, &compiledStmt, NULL) == SQLITE_OK)
{
UIImage *image = [UIImage imageNamed:@"vegextra.png"];
NSData *imageData=UIImagePNGRepresentation(image);
sqlite3_bind_blob(compiledStmt, 1, [imageData bytes], [imageData length], SQLITE_TRANSIENT);
if(sqlite3_step(compiledStmt) != SQLITE_DONE) {
NSLog(@"Error: %s", sqlite3_errmsg(db));
} else {
NSLog(@"Insert into row id = %lld", (sqlite3_last_insert_rowid(db)));
}
sqlite3_finalize(compiledStmt);
}
}
sqlite3_close(db);
}
- (void)showImage
{
sqlite3_stmt *compiledStmt;
sqlite3 *db;
int i = 1;
if(sqlite3_open([dbPath UTF8String], &db)==SQLITE_OK)
{
NSString *insertSQL = [NSString stringWithFormat:@"Select image from Image Where Id = %d",i];
if(sqlite3_prepare_v2(db,[insertSQL cStringUsingEncoding:NSUTF8StringEncoding], -1, &compiledStmt, NULL) == SQLITE_OK)
{
while(sqlite3_step(compiledStmt) == SQLITE_ROW) {
int length = sqlite3_column_bytes(compiledStmt, 0);
NSData *imageData = [NSData dataWithBytes:sqlite3_column_blob(compiledStmt, 0) length:length];
NSLog(@"Length : %d", [imageData length]);
if(imageData == nil)
NSLog(@"No image found.");
else
imgView.image = [UIImage imageWithData:imageData];
}
}
sqlite3_finalize(compiledStmt);
}
sqlite3_close(db);
}
我在这里需要存储和检索PDF与图像相同。
与新核心数据“保存在外部记录文件”功能相比,“保留文件系统中的文件”如何? – user523234 2013-04-28 07:55:02
实际上,如果文件小于大约100 KB,那么将数据存储在数据库中效率更高(http://www.sqlite.org/intern-v-extern-blob.html)。 – 2013-04-28 08:03:41