来自Sqlite数据库的UITableview图像

时间:2015-10-11 06:09:55

标签: ios objective-c sqlite uitableview

我想在SQLite的tableView中显示图像。这是我从SQLite代码中检索图像代码。如何在tableview中加载图像?

-(void) retrieveData{
sqlite3 * database;
NSMutableArray *prodnamemutable = [[NSMutableArray alloc] init];
NSMutableArray *prodpricemutable = [[NSMutableArray alloc] init];
NSMutableArray *prodimagemutable = [[NSMutableArray alloc] init];
NSString *databasename=@"contacts.sqlite";  // Your database Name.

NSArray * documentpath=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSAllDomainsMask, YES);

NSString * DocDir=[documentpath objectAtIndex:0];

NSString * databasepath=[DocDir stringByAppendingPathComponent:databasename];

if(sqlite3_open([databasepath UTF8String], &database) == SQLITE_OK)
{
    const char *sqlStatement = "SELECT * FROM contacts";  // Your Tablename

    sqlite3_stmt *compiledStatement;

    if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK)
    {
        [prodnamemutable removeAllObjects];

        while(sqlite3_step(compiledStatement) == SQLITE_ROW)
        {

            [prodnamemutable addObject:[NSString stringWithFormat:@"%s",(char *) sqlite3_column_text(compiledStatement, 1)]];
            [prodpricemutable addObject:[NSString stringWithFormat:@"%s",(char *) sqlite3_column_text(compiledStatement, 3)]];

            int length = sqlite3_column_bytes(compiledStatement, 0);
            NSData *imageData = [NSData dataWithBytes:sqlite3_column_blob(compiledStatement, 0) length:length];
            [prodimagemutable addObject:imageData];
            NSLog(@"Length from db : %lu", (unsigned long)[imageData length]);
            imageArray = [NSArray arrayWithArray:prodimagemutable];
            NSLog(@"image found.%@",imageArray);
        }
    }
    nameArray = [[NSArray alloc]initWithArray:prodnamemutable];
    priceArray = [[NSArray alloc]initWithArray:prodpricemutable];
    NSLog(@"nameArray:%@",nameArray);
    [[self navigationController] tabBarItem].badgeValue = [NSString stringWithFormat:@"%lu",(unsigned long)[nameArray count]];
    sqlite3_finalize(compiledStatement);
    [self.tableView reloadData];

}
sqlite3_close(database);

}

2 个答案:

答案 0 :(得分:0)

UIImage *image = [UIImage imageWithData:data];

这会将您的图片NSData转换为UIImage,现在此图片可以显示在任何UIImageView上。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;    //count of section
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return [prodimagemutable count];    //count number of row from image array.
}



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

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"];

        if (cell == nil)
        {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                           reuseIdentifier:MyIdentifier];
        }
        cell.imageView.image = [UIImage imageWithData:[prodimagemutable objectAtIndex: indexpath.row]] ;
        return cell;
    }
//Below use for set height of cell

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

        return 80;// or the height you want.

}

答案 1 :(得分:0)

如果将代码分成多个类(例如以下

)会更好

<强> 1。 DBManager

return HttpResponseRedirect("{}?vendor_id={}".format(reverse('vendor_data'), vendor_id))

<强> 2。 ImagesTableViewController

@interface DBManager : NSObject
- (NSArray *)getImages;
@end

@implementation DBManager
- (NSArray *)getImages {
    // put your code to retrieve the images from the database
    // use the following to convert from NSData to UIImage
    UIImage *image = [UIImage imageWithData:imageData];
}
@end