【问题标题】:Adding UIImageView to UIScrollView in background Thread, wont show up until all are added在后台线程中将 UIImageView 添加到 UIScrollView,直到全部添加后才会显示
【发布时间】:2010-12-21 13:07:44
【问题描述】:

我有一个滚动视图,我在主线程中添加了一些 UIButtons 和 UIActivityIndi​​cators。 然后我执行 [NSThread detachSelectorInBackground:@selector(getImages) toTarget:self withObject:nil];

getImages 下载一些图像并将它们添加到 UIImageViews 中,然后将 UIImageViews 添加到滚动视图中,但是直到 getImages 方法完成后它们才会显示出来。 有没有办法让 scrollView 重绘或刷新或类似的东西?

与 如果我在 getImages 方法期间滚动滚动视图(用我的手指..),则会显示已添加的 UIImageViews。

- (void) getImages {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    int x=5;
    int y=0;
    NSData *imgData;
    UIImage *img;
    UIImageView *imgView;
    for (NSDictionary *tmp in thumbs) {

  imgData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:[Functions urlForFile:[tmp objectForKey:@"img"]]]];
  if ([imgData length] > 0) {
    img = [[UIImage alloc] initWithData:imgData];
    imgView = [[UIImageView alloc] initWithFrame:CGRectMake(x, y + (133-img.size.height)/2, 100, img.size.height)];

    imgView.image = img;
    [imgView setAlpha:0.0];
    [[self.scrollView viewWithTag:1500000*[[tmp objectForKey:@"id"] intValue]] setAlpha:1.0];
    [self.scrollView addSubview:imgView];

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:2.0];
    [[self.scrollView viewWithTag:1500000*[[tmp objectForKey:@"id"] intValue]] setAlpha:0.0];
    [imgView setAlpha:1.0];
    [UIView commitAnimations];
    [[self.scrollView viewWithTag:1500000*[[tmp objectForKey:@"id"] intValue]] removeFromSuperview];      

    [imgView release];
    [img release];
    [imgData release];

    x+=105;
    if (x > 300) {
      x=5;
      y+=138;
    }
  }
}
[pool release];
[appDelegate.loadingView setHidden:YES];
}

【问题讨论】:

    标签: iphone uiscrollview uiimageview redraw


    【解决方案1】:

    在后台更改 UI 不是线程安全的!在后台下载但不显示。要一张一张地显示图像,您可以尝试使用performSelectorOnMainThread:@selector() withObject:nil waitUntilDone:YES 运行它 例如self.scrollView addSubview:

    但同样,UI 应该始终使用 MainThread(例如动画)

    【讨论】:

      猜你喜欢
      • 2015-06-29
      • 1970-01-01
      • 2012-02-14
      • 1970-01-01
      • 2011-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多