Добавление UIImageView к UIScrollView в фоновом потоке не будет отображаться, пока все не будут добавлены - PullRequest
1 голос
/ 21 декабря 2010

У меня есть scrollView, где я добавляю некоторые UIButtons и UIActivityIndicators в основной поток.Затем я выполняю [NSThread detachSelectorInBackground: @selector (getImages) toTarget: self withObject: nil];

getImages загружает некоторые изображения и добавляет их в UIImageViews, а затем добавляет UIImageViews в scrollView, но они не отображаютсяпока метод getImages не будет выполнен.Есть ли способ заставить scrollView перерисовать или обновить или что-то в этом роде?

И если я прокручиваю 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];
}

1 Ответ

1 голос
/ 21 декабря 2010

изменение пользовательского интерфейса в фоновом режиме не является потокобезопасным! Загрузка в фоновом режиме, но не отображается. Чтобы показать изображение одно за другим, вы можете попробовать запустить его с performSelectorOnMainThread:@selector() withObject:nil waitUntilDone:YES например с self.scrollView addSubview:.

Но опять же, пользовательский интерфейс всегда должен использовать MainThread (например, анимацию)

...