多个图像未显示在数据库中

时间:2013-08-08 04:48:55

标签: iphone database uiimageview fetch

我将所有图像存储在SQLiteManager中以供本地数据库使用。我需要在viewcontroller中显示所有图像。但是,当我从数据库中提取图像时,它只显示一个图像。我不使用任何web服务。

代码:

的SQLiteManager:

Table name: SimpleTbl


      id    sm     descrip        photo
        1      sm1    ok            BLOB(size:2345)

        2      sm2    ok1            BLOB(size:3245)

        3      sm3    ok2            BLOB(size:4535)


.h file:


@interface Mysof : NSObject{
    NSInteger sofId;
    NSString *sof;
    NSString *rating;
    UIImage *photo;
}

@property (nonatomic,retain)NSString *sofa;
@property (nonatomic, assign) NSInteger sofaId;
@property (nonatomic, retain)NSString *rating;
@property (nonatomic, retain) UIImage *photo;

.m文件:

- (NSMutableArray *) getMylists{
    NSMutableArray *sArray = [[NSMutableArray alloc] init];
    @try {
        NSFileManager *fileMgr = [NSFileManager defaultManager];
        NSString *dbPath = [[[NSBundle mainBundle] resourcePath ]stringByAppendingPathComponent:@"Sample.sqlite"];
        BOOL success = [fileMgr fileExistsAtPath:dbPath];
        if(!success)
        {
            NSLog(@"Cannot locate database file '%@'.", dbPath);
        }
        if(!(sqlite3_open([dbPath UTF8String], &db) == SQLITE_OK))
        {
            NSLog(@"An error has occured.");
        }

      ;


        const char *sql = "SELECT * FROM SimpleTbl";       


        NSLog(@"sql is %s", sql);

        sqlite3_stmt *sqlStatement;
        if(sqlite3_prepare(db, sql, -1, &sqlStatement, NULL) != SQLITE_OK)
        {
            NSLog(@"Problem with prepare statement");
        }

        //
        while (sqlite3_step(sqlStatement)==SQLITE_ROW) {
            Mysof *Mylist = [[Mysof alloc]init];
            Mylist.sofId = sqlite3_column_int(sqlStatement, 0);
            Mylist.sof = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement,1)];
            Mylist.rating = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement, 2)];
            const char *raw = sqlite3_column_blob(sqlStatement, 3);
            int rawLen = sqlite3_column_bytes(sqlStatement, 3);
            NSData *data = [NSData dataWithBytes:raw length:rawLen];
            Mylist.photo = [[UIImage alloc] initWithData:data];
            [sArray addObject:Mylist];
        }
    }
    @catch (NSException *exception) {
        NSLog(@"An exception occured: %@", [exception reason]);
    }
    @finally {
        return sArray;
    }

}

然后viewcontroller我通过按钮点击显示提取图像到imageview:

-(void)click:(id)sender{

 mmageView=[[UIImageView alloc]initWithFrame:CGRectMake(20, 52, 72, 72)];

        // UIImageView *mmageView=[UIImageView alloc];

        // [mmageView setFrame:CGRectMake(20, 52, 75, 75)];
        [self.view addSubview:mmageView];

        Soflistsql * mysofs =[[Soflistsql alloc] init];
        self.arraysofs = [mysofs getMyLists];
        [mmageView setImage:((Mysof *) [self.arraysofs objectAtIndex:0]).photo];

}

滚动视图:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGPoint scrollOffset=scrollView.contentOffset;


    ///at any time, it will have only 3 pages loaded- previous, current and next
    if(pageOnScrollView < ((int)scrollOffset.x/320))
    {
        //unload
        if(pageOnScrollView>1)[self unloadPreviousPage:pageOnScrollView-2];
[self loadNextPage:((int)scrollOffset.x/320)+1];
else if(pageOnScrollView > ((int)scrollOffset.x/320))
    {
        //unload
        if(pageOnScrollView<(NUM_PAGES-2))[self unloadPreviousPage:pageOnScrollView+2];
 [self loadNextPage:((int)scrollOffset.x/320)-1];
 }

    pageOnScrollView=scrollOffset.x/320;
}

-(void)unloadPreviousPage:(int)index
{


 for (int index = 0; index<[self.arraysofs count]; index++ ) {

        [[myScrollView viewWithTag:index+1] removeFromSuperview];

          }

}

-(void)loadNextPage:(int)index
{

  int countFlag=0;

    for (int index = 0; index<[self.arraysofs count]; index++ ) {
        NSLog(@"index %d",index);




        mmageView=[[UIImageView alloc]initWithFrame:CGRectMake((320*index)+countFlag*80+ 2, 5, 75, 75)];

        [self.view addSubview:mmageView];

        mmageView.tag=index+1;


        [mmageView setImage:((Mysof *) [self.arraysofs objectAtIndex:index]).photo];



    }

    [myScrollView addSubview:mmageView];

     countFlag++;

}

1 个答案:

答案 0 :(得分:1)

1)记录数组计数并检查项目计数。

2)然后像这样制作一个for循环

for (int index = 0; index < [self.arraysofs count]; index ++ ) {
         NSLog(@"index %d",index);

        mmageView=[[UIImageView alloc]initWithFrame:CGRectMake(20+(index*74), 52, 72, 72)];

        [self.view addSubview:mmageView];

        Soflistsql * mysofs =[[Soflistsql alloc] init];
        self.arraysofs = [mysofs getMyLists];
        [mmageView setImage:((Mysof *) [self.arraysofs objectAtIndex:index]).photo];
    }

选中此项以在滚动视图Adding UIImageViews to UIScrollView

上添加图片