将BLOB对象加载到iPhone中的数组中

时间:2010-09-05 06:54:08

标签: iphone sqlite nsdata

我需要使用从SQLite DB中重新获取的BLOB类型数据来填充数组。目前我使用NSData来显示图像,但我想将图像放在数组中并将其显示在UITableView中

我该怎么做,请帮帮我

1 个答案:

答案 0 :(得分:0)


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *CellIdentifier = @"Cell";

    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) 
    { 
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }
    else 
    {
        UIView* subview;
        while ((subview = [[[cell contentView] subviews] lastObject]) != nil)
            [subview removeFromSuperview];
    }

    UIView *vewmanulogo=[[UIView alloc]initWithFrame:CGRectMake(25,8,6,2)];

    sql=nil;
    stmt=nil;
    sql="select ManufacturerLogo from Manufacturer_tbl where ManufacturerID=?";
    sqlite3_prepare_v2(db, sql, -1, & stmt, NULL);
    NSLog(@"%@",manufacturerid);
    sqlite3_bind_int(stmt, 1,[ [manufacturerid objectAtIndex:indexPath.row]intValue]);    //manufacturer id is an array

    while(sqlite3_step(stmt)==SQLITE_ROW)
    {


        NSData *data = [[NSData alloc] initWithBytes:sqlite3_column_blob(stmt, 0) length:sqlite3_column_bytes(stmt, 0)];
            if(data == nil)
            {
                NSLog(@"No image found.");
            }
            else
            {

                [manufacturerlogo addObject:data];    //manufacturerlogo is an array

            }
    }
    UIImage *image = [UIImage imageWithData:[manufacturerlogo objectAtIndex:indexPath.row]]; //extracting individual images and assigning it to the UIImage
    UIImageView *manulogvw=[[UIImageView alloc]initWithImage:image];


    cell.accessoryType=UITableViewCellAccessoryDetailDisclosureButton;
    cell.selectionStyle=UITableViewCellSelectionStyleGray;
    [vewmanulogo addSubview:manulogvw];  //adding the image view to the view and making it as an content view to the cell.
    [cell.contentView addSubview:vewmanulogo];
    return cell;
}