【问题标题】:How to display a BLOB image that is taken from database in the imageview?如何在 imageview 中显示从数据库中获取的 BLOB 图像?
【发布时间】:2012-05-02 04:08:43
【问题描述】:

我将图像作为 BLOB 存储在数据库中,并使用以下编码获取它

const char*dbPath=[databasePath UTF8String];
if (sqlite3_open(dbPath, &sqliteHandler)==SQLITE_OK) {
    sqlite3_stmt *stmt;
    NSString *selctQuery=[NSString stringWithFormat:@"select * from imagetable where imageId=11"];
    NSLog(@"selectQuery is %@",selctQuery);
    const char *dispStmt=[selctQuery UTF8String];       
    sqlite3_prepare_v2(sqliteHandler, dispStmt, -1, &stmt, NULL);
    NSLog(@"%d",sqlite3_prepare_v2(sqliteHandler, dispStmt, -1, &stmt, NULL));

    while (sqlite3_step(stmt)==SQLITE_ROW) {
        //  NSString *pwd=[[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(sqltmt, 1)];
    //  NSString *image1=[[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(stmt, 1)];
    //  NSString *image2=[[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(stmt, 2)];
    //  NSString *image3=[[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(stmt, 3)];
        //NSLog(@"%@",image1);
        NSData *cachedImage1 = [[NSData alloc] initWithBytes:sqlite3_column_blob(stmt, 1) length: sqlite3_column_bytes(stmt, 1)];           
        NSData *cachedImage2 = [[NSData alloc] initWithBytes:sqlite3_column_blob(stmt, 2) length: sqlite3_column_bytes(stmt, 2)];           
        NSData *cachedImage3 = [[NSData alloc] initWithBytes:sqlite3_column_blob(stmt, 3) length: sqlite3_column_bytes(stmt, 3)];           

        thumbImage = [UIImage imageWithData:cachedImage1];
        bgImage = [UIImage imageWithData:cachedImage2];
        logoImage = [UIImage imageWithData:cachedImage3];
        [cachedImage1 release];
        [cachedImage2 release];
        [cachedImage3 release];

    }
}

我尝试使用以下代码显示...

bCardFront.image=[UIImage imageNamed:bgImage]

但它显示警告消息说

从不同的 Objective-C 类型传递“imageNamed:”的参数 1 时,Objective-C 类型“struct UIImage *”不兼容,预期为“struct NSString *”

谁能告诉我如何在单独的图像视图中显示这些图像。

提前致谢。

【问题讨论】:

    标签: iphone objective-c xcode blob


    【解决方案1】:

    bCardFront.image=[UIImage imageNamed:bgImage]替换为

    bCardFront.image=bgImage 这里的 bgImage 不是图像名称它是 UIImage 的对象

    【讨论】:

    • 我试过这个,但它不起作用。而且也没有显示任何错误。该应用程序刚刚退出。
    • 我是手动存储的,而不是通过代码存储...我确信图像被正确获取。因为当我尝试为按钮设置背景图像时,它的工作,但同一图像在 ImageViews 中不起作用。
    • @iPhone_suren 然后检查 bCardFront 启动并检查是否为捆绑包设置图像,然后告诉
    【解决方案2】:

    [UIImage imagedNamed:xxx] 将从您的应用程序包中获取图像,xxx 是图像名称的字符串。试试这个:

    [bCardFront setImage:bgImage];
    

    【讨论】:

    • 我试过这个,但它不起作用。而且也没有显示任何错误。该应用程序刚刚退出。
    猜你喜欢
    • 2019-04-19
    • 2016-11-16
    • 1970-01-01
    • 2018-11-26
    • 1970-01-01
    • 1970-01-01
    • 2017-02-09
    • 2020-12-16
    • 2013-01-23
    相关资源
    最近更新 更多