0

我将所有图像存储在本地数据库的 SQLiteManager 中。我需要在视图控制器中显示所有图像。但是当我从数据库中获取图像时,它只显示一张图像。我不使用任何网络服务。

代码:

SQLite 管理器:

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++;

}
4

1 回答 1

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];
    }

选中此项以在滚动视图上添加图像将 UIImageViews 添加到 UIScrollView

于 2013-08-08T05:09:25.467 回答