无法在iphone中显示sqlite数据库(blob)中的图像

时间:2015-03-02 17:05:53

标签: ios database image sqlite blob

我试图从我的sqlite数据库(作为blob)显示图像,它通过显示图像的长度和NSLog知道图像文件在那里:" Image FOUND" ,但它不会在imageview中加载图像。我已经尝试了像stack {this one这样的堆栈流程中的那些例子以及更多其他但仍然没有问题。谢谢你们。这是我到目前为止所得到的:

sqlite3_stmt *statement1;

NSString *querySQL1 = [NSString stringWithFormat:@"SELECT picture FROM table1 WHERE var = %f", variable];

const char *query_statement1 = [querySQL1 UTF8String];

if(sqlite3_prepare_v2(_DB, query_statement1, -1, &statement1, nil) == SQLITE_OK)
{

    if (sqlite3_step(statement1) == SQLITE_ROW)
    {

    int length = sqlite3_column_bytes(statement1, 0);

    NSData *data = [[NSData alloc] initWithBytes:sqlite3_column_blob(statement1, 0) length:length];

        UIImage *image = [UIImage imageWithData:data];

        NSLog(@"Length : %d", [data length]);

        if(data == nil)
            {
                NSLog(@"No image found.");
            }

        else
            {
                NSLog(@"image FOUND .");
                myImageView.image = image;       
            }   
    }

}

1 个答案:

答案 0 :(得分:0)

sqlite3 *dataname1;

if (sqlite3_open([datapath UTF8String],&dataname1) == SQLITE_OK) {

    const char *sqlStatement;

    sqlStatement = "SELECT * FROM photography_tips";


    sqlite3_stmt *compiledStatement;

    if(sqlite3_prepare_v2(dataname1, 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
        NSData *data = [[NSData alloc] initWithBytes:sqlite3_column_blob(compiledStatement, 2) length:sqlite3_column_bytes(compiledStatement, 2)];
            [tipimage addObject:data];

        }

    }
NSLog(@"%@", tiptittle);
}

然后在cellforrowatindexpath方法中添加以下代码

 NSData *data= (NSData *) [tipimage objectAtIndex:indexPath.row];
cell.imageView.image=[UIImage imageWithData:data];