NSImage转换为blob,以及blob转换为NSImage(涉及SQLite和NSData)

7

首先,我知道不应该使用SQLite数据库来存储图像,但我只是存储网站的favicon,它们非常小。

我的问题是,我尝试将这些favicon插入到数据库中(似乎可以工作),我使用NSImage-tiffRepresentation方法将favicon转换为NSData,然后将其插入到我的数据库中的blob列中:

 NSImage *favico = [webview mainFrameIcon];
[appDelegate insertBookmark:[titleField stringValue] url:[urlfield stringValue] data:[favico TIFFRepresentation]]

SQLite的方法如下所示:
-(void)insertBookmark:(NSString *)title url:(NSString *)url data:(NSData *)data
{
    NSData *imagedata = [[NSData alloc]initWithData:data]; 
    sqlite3 *database;
    if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
        NSString *query = [NSString stringWithFormat:@"INSERT INTO Bookmarks (title, url, image) VALUES ('%@', '%@', '%@')",title, url, data];
        const char *querychar = [query UTF8String]; 
        sqlite3_stmt *statement; 
        if (sqlite3_prepare_v2(database, querychar, -1, &statement, NULL) == SQLITE_OK)
        {
            int row =  3;
            sqlite3_bind_blob(statement, row, [imagedata bytes], [imagedata length], NULL);
            sqlite3_step(statement);
            sqlite3_finalize(statement);

        }
        else
        {
            NSLog(@"Error"); 
        }

        [databasePath retain]; 
        [databaseName retain]; 
    }
    sqlite3_close(database);
    [imagedata release]; 

}

当我查看数据库时,我发现在图像列中有<data>的值(正常吗?)。
现在,当我从数据库中提取blob并尝试将其放入对象中时,我的NSImage为null。
-(void) readDatabase {
// Setup the database object
sqlite3 *database;

bookmarks = [[NSMutableArray alloc] init];

// Open the database from the users filessytem
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
    // Setup the SQL Statement and compile it for faster access
    const char *sqlStatement = "SELECT * FROM Bookmarks ORDER BY title ASC";
    sqlite3_stmt *compiledStatement;
    if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
        // Loop through the results and add them to the feeds array
        while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
            // Read the data from the result row
            NSString *aTitle = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];
            NSString *aUrl = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)];
            NSData *data = [[NSData alloc] initWithBytes:sqlite3_column_blob(compiledStatement, 3) length:sqlite3_column_bytes(compiledStatement, 3)];
            NSImage *image = [[NSImage alloc]initWithData:data];
            bookmarkObject *bookmark = [[bookmarkObject alloc] initWithName:aTitle url:aUrl favico:image]; 

            [bookmarks addObject:bookmark];
            [bookmark release];
            [data release];
            [image release]; 



        }
    }
    // Release the compiled statement from memory
    sqlite3_finalize(compiledStatement);
    sqlite3_close(database); 
    [databasePath retain]; 
    [databaseName retain]; 


}

}

Thanks in advance

1个回答

4
你实际数据周围的<>来自NSData。通过在你的格式字符串中使用%@并提供dataNSString会向data发送description[NSData description]将内容包装在<>之间。
你代码的另一个问题是,你似乎混合了无参数和参数准备好的语句:
  • 要么使用stringWithFormat:创建完整的查询语句
  • 或者使用类似于INSERT INTO Bookmarks(title ...)VALUES(?...)的查询与sqlite3_bind_blob组合
SQLite支持的参数语法可以在此处找到:http://www.sqlite.org/c3ref/bind_blob.html

谢谢!我已经纠正了我的代码,并使用(?)语法和绑定将所有内容放入查询字符中,现在它可以工作了!问题出在NSString的数据上。 - Dimillian

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接