【问题标题】:Multiple images not showing from database多张图片未从数据库中显示
【发布时间】:2013-08-08 13:54:31
【问题描述】:

我将所有图像存储在本地数据库的 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++;

}

【问题讨论】:

  • 记录数组并检查有多少对象。我认为您将图像设置为图像视图的第 0 个索引,您如何期望在不使用某些逻辑来显示其他图像的情况下显示其他图像。
  • 你的意思是我需要将 self.arraysofs 检查到日志中。我需要将所有图像显示到滚动视图
  • 您的 click 方法仅返回 0 的 objectAtIndex(第一项)。除了索引 0 处的对象之外,您还没有与我们共享任何可以从 arraysofs 检索任何内容的代码。
  • @Gup:检查我编辑的问题以获取 NSLog 中的数组
  • @Rob:我分享了所有代码。当我使用 objectAtIndex1 它显示第二个图像。这里如何使用for循环来显示所有图片?

标签: iphone database uiimageview fetch


【解决方案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

【讨论】:

  • 当点击按钮for循环没有执行并且记录数组为零(0)
  • self.arraysofs 显示为空
  • 你是对的。它现在正在工作。如何用滚动视图显示这些数组。简单添加 [scroll addsubview:mmageView]
  • 查看我为滚动视图编辑的问题。但它只显示一张图片
  • 设置这个 [scrollView setContentSize:CGSizeMake([self.arraysofs count]*74 +indent if any, height)];
猜你喜欢
  • 2011-04-23
  • 2019-11-16
  • 2013-08-09
  • 2016-04-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-24
  • 2021-10-30
相关资源
最近更新 更多