【问题标题】:Display image from MySQL database in a UITableView iPhone在 UITableView iPhone 中显示 MySQL 数据库中的图像
【发布时间】:2010-07-21 05:38:05
【问题描述】:

我有一些图像数据以 BLOB 格式存储在 MySQL 数据库中, 我获取字符串和整数数据,但对于图像它不显示。 我也尝试过使用UIImageView,但它不起作用。

有人可以发布一些代码以在UITableView 中显示图像数据。 NSData-->UIImage-->图片展示。

谢谢

【问题讨论】:

    标签: iphone mysql uitableview image uiimageview


    【解决方案1】:

    给定一个 NSData 对象 data,其中数据是 UIImage 支持的图像类型(PNG、JPG、...)

    /* These two are what I assume you start with */
    char *rawData = [self getRawDataFromDB];
    int rawDataLength = [self getLengthOfRawData];
    
    /* To convert that to an image... */
    UIImage *image = [UIImage imageWithData:[NSData dataWithBytes:rawData length:rawDataLength]];
    

    一旦你有了图片,然后添加到你的 UITableViewDataSource 的tableView:cellForRowAtIndexPath:

    [[cell imageView] setImage: image];
    

    【讨论】:

    • 在数据库获取结果后我做了 for(NSDictionary *row in result) { [tableData addObject:[row objectForKey:@"imgdata"]];并在表视图方法 [cell.imageView setImage:[tableData objectAtIndex:index]];你能解释一下你的代码放在哪里
    • 你可以把它放在任何一个地方,但你需要将 [row objectForKey:@"imgdata"] (这是 rawData) 转换为 UIImage。我将它作为 [tableData addObject:image] 放入数据库获取例程中。
    • 感谢您的回复。如何将原始数据转换为图像?您能用一段代码解释一下吗...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-07
    • 2019-02-22
    • 2018-01-24
    • 2020-05-19
    相关资源
    最近更新 更多