gpt4 book ai didi

objective-c - NSImage 到 blob 和 blob 到 NSImage (SQLite, NSData)

转载 作者:IT王子 更新时间:2023-10-29 06:24:13 27 4
gpt4 key购买 nike

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

我的问题是我尝试将这些图标插入数据库(似乎有效),我将图标转换为 NSData-tiffrepresentation NSimage的方法|然后将其插入到我的数据库中的 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];


}

提前致谢

最佳答案

<>你的实际数据来自 NSData。通过使用 %@在您的格式字符串中并提供 data , NSString发送 descriptiondata . [NSData description]将内容包装在 < 之间和 > .

您的代码的另一个问题是,您似乎混合了无参数和参数准备语句:

  • 要么使用stringWithFormat:创建一个完整的查询语句
  • 或使用类似 INSERT INTO Bookmarks (title ...) VALUES (? ...) 的查询结合 sqlite3_bind_blob

SQLite 支持的参数语法可以在这里找到: http://www.sqlite.org/c3ref/bind_blob.html

关于objective-c - NSImage 到 blob 和 blob 到 NSImage (SQLite, NSData),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5847514/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com