1

いくつかの画像を読み込んでいる UIScrollView があります。画像に効果を適用することがあり、プリロードを行うのに少し時間がかかることがあるので、detachNewThreadSelector を使用して別のスレッドでこれを行うことにしました。これにはgitHubにあるKTPhotoBrowserを使用しています。

基本的に、私はそのように機能しています。

- (void)setCurrentIndex:(NSNumber *)newIndex
{

   NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

   currentIndex_ = [newIndex integerValue];

   [self loadPhoto:currentIndex_];
   [self loadPhoto:currentIndex_ + 1];
   [self loadPhoto:currentIndex_ - 1];
   [self unloadPhoto:currentIndex_ + 2];
   [self unloadPhoto:currentIndex_ - 2];

  [self setTitleWithCurrentPhotoIndex];
  [self toggleNavButtons];
  [pool release];

}

私はこれを使用して呼び出します

[NSThread detachNewThreadSelector:@selector(setCurrentIndex:) toTarget:self withObject:[NSNumber numberWithInt:5]];

これを実行すると、リークが発生しているように見えます。loadPhoto メソッドのコードの周りに AutoRelease プールを配置する必要があるかどうか疑問に思い始めています。このコードに興味がある場合は、以下に含めます。

- (void)loadPhoto:(NSInteger)index
{
   if (index < 0 || index >= photoCount_) {
      return;
   }

   id currentPhotoView = [photoViews_ objectAtIndex:index];
   if (NO == [currentPhotoView isKindOfClass:[KTPhotoView class]]) {
      // Load the photo view.
      CGRect frame = [self frameForPageAtIndex:index];
      KTPhotoView *photoView = [[KTPhotoView alloc] initWithFrame:frame];
      [photoView setScroller:self];
      [photoView setIndex:index];
      [photoView setBackgroundColor:[UIColor clearColor]];

      // Set the photo image.
      if (dataSource_) {
         if ([dataSource_ respondsToSelector:@selector(imageAtIndex:photoView:)] ==    NO) {
            UIImage *image = [dataSource_ imageAtIndex:index];
            [photoView setImage:image];
         } else {
        [dataSource_ imageAtIndex:index photoView:photoView];
         }
      }

      [scrollView_ addSubview:photoView];
      [photoViews_ replaceObjectAtIndex:index withObject:photoView];
      [photoView release];
   } else {
      // Turn off zooming.
      [currentPhotoView turnOffZoom];
   }
}

どんなアイデアでも大歓迎です。

4

2 に答える 2

0

以下を使用してください

[self performSelectorInBackground:@selector(setCurrentIndex:) withObject:[NSNumber numberWithInt:5]];

それ以外の

[NSThread detachNewThreadSelector:@selector(setCurrentIndex:) toTarget:self withObject:[NSNumber numberWithInt:5]];

そしてそれはメモリリークを排除します。

于 2012-11-02T10:08:56.057 に答える
0

あなたのコードは問題ないようですが、別のスレッドから UIKit を使用しています。UIKit クラスは、アプリケーションのメイン スレッドからのみ使用する必要があります。

UIKit フレームワーク リファレンスの紹介

于 2011-02-23T21:52:30.503 に答える