如何在检索数据库时在 UIImage 视图中显示 NSData 值

how to show the NSData value in UIImage View while retrieving the database

已成功将图像作为 NSData 添加到数据库中。但是,当我将 NSData 检索回 UIImage 时,它​​不起作用。以下是我用来检索数据库的编码,如有错误请建议我。

这里我使用主键来获取特定记录中显示的正确图像:

-(void)ShowImagesinImageView{

    NSLog(@"Loaded in productlist");
    NSLog(@"Primary Key of Expenses ID=%@",primarykeyID);

    proID      = [[NSMutableArray alloc] init];
    NSInteger proeditid = [primarykeyID integerValue];
    int len = 0;

    NSString *queryStr = [NSString stringWithFormat:@"select image from selection_tbl where expenses_id=?"];
    Dataware *dbsql    = [[Dataware alloc] initDataware];
    sqlite3_stmt *sqlStmt=[dbsql OpenSQL:[queryStr UTF8String]];

    Dataware *DB = [[Dataware alloc]initDataware];
    sqlite3_stmt *stmt=[DB OpenSQL:[queryStr UTF8String]];
    if(sqlStmt != nil)
        while(sqlite3_step(sqlStmt) == SQLITE_ROW)
        {
            len = sqlite3_column_bytes(stmt, 0);
            NSData *imagData = [[NSData alloc] initWithBytes: sqlite3_column_blob(stmt, 0) length: len];

            sqlite3_bind_int(stmt,1, (int)proeditid);

            UIImage *img = [[UIImage alloc] initWithData:imagData];
            imageView.image = img;

        }
    [dbsql CloseSQL];

    [tblview reloadData];

}

您使用了非常复杂的方法从数据库中获取数据

用了这个就很容易理解了

-(void)getListOfUnitsNameByCategory1{

    sqlite3_stmt *statement;

    NSInteger proeditid = [primarykeyID integerValue];

    int len = 0;

    NSString *queryString=[NSString stringWithFormat: @"select image from selection_tbl where expenses_id=%ld",(long)proeditid];

    if( sqlite3_prepare_v2(_database, [queryString UTF8String], -1, &statement, NULL)== SQLITE_OK)
    {
        while (sqlite3_step(statement) <= SQLITE_ROW) {
            len = sqlite3_column_bytes(statement, 0);
            NSData *imagData = [[NSData alloc] initWithBytes: sqlite3_column_blob(statement, 0) length: len];

            UIImage *img = [[UIImage alloc] initWithData:imagData];
            imageView.image = img;
        }
        sqlite3_finalize(statement);

       [tblview reloadData];

    }
}

这对你有帮助

NSString *queryStr = [NSString stringWithFormat:@"select image from selection_tbl where expenses_id=%ld",(long)proeditid];

    Dataware *dbsql    = [[Dataware alloc] initDataware];
    sqlite3_stmt *sqlStmt=[dbsql OpenSQL:[queryStr UTF8String]];

    if(sqlStmt != nil)
        while(sqlite3_step(sqlStmt) == SQLITE_ROW)
        {
            NSData *dataForCachedImage = [[NSData alloc] initWithBytes:sqlite3_column_blob(sqlStmt, 0) length: sqlite3_column_bytes(sqlStmt, 0)];
            UIImage *img;
            img = [UIImage imageWithData:dataForCachedImage];
            imageView.image =img;
        }
    [dbsql CloseSQL];
    [tblview reloadData];

我想是的。