0

バックグラウンド 5 スレッドがほぼ同時に画像をロードしているため、このコードはメモリ不足の警告を生成します。

各スレッドに優先順位をつけてロック&アンロックしたい。ステップバイステップのスレッドを作成します。画像 1 の読み込み -> 画像 2 の読み込み -> 画像 3 の読み込み -> 画像 4 の読み込み。

これどうやってするの?

ビューコントローラー

-(void)viewDidLoad
{            
 for(int i=0; i<screenshotcount ; i++)
 {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];  
    NSString * url=[detailItem.mScreenshot objectAtIndex:i];
    NSDictionary *args=[NSDictionary dictionaryWithObjectsAndKeys:
                       [NSNumber numberWithInt:i], @"screenNum",
                       [NSString stringWithString:url],@"url",
                       nil];
    [self performSelectorInBackground:@selector(loadImageScreenshot:) withObject:args];
    [pool release]; 
  }
}

読み込み中の画像

-(void) loadImageScreenshot:(NSDictionary *) args
{
 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 
 UIImage * screenshotImage=[UIImage imageWithStringURL:url];
 NSDictionary *args2=[NSDictionary dictionaryWithObjectsAndKeys:
                    [NSNumber numberWithInt:num], @"screenNum",
                    screenshotImage,@"image",
                    nil];                                                               

[self performSelectorOnMainThread:@selector(assignImageToScreenshotImageView:) withObject:args2  waitUntilDone:YES];
[pool release];
}

画像追加

- (void) assignImageToScreenshotImageView:(NSDictionary *)arg
{ 
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
UIImage * image= [arg objectForKey:@"image"];
UIImageView *imageview=[UIImageView alloc]init];
               .
               .
imageview.image=image;
[self.mScreenshotSpace addSubview:imageview];
[imageview release];
[pool release];
}

URLからの画像

+(UIImage *)imageWithStringURL:(NSString *)strURL
{
 NSURL *url =[NSURL URLWithString:strURL];
 NSData *   data=[[NSData alloc]initWithContentsOfURL:url options:NSDataReadingUncached error:&error];

 UIImage * image=[UIImage imageWithData:data ];
 [data release];
 return image;
}
4

2 に答える 2

0

あなたの質問を誤解したかもしれませんが、あなたが言ったことから、あなたが本当に望んでいるのは、スレッドを「シリアル化」することです。つまり、スレッドが次々に実行されるようにすることです。この場合、5つ(またはそれ以上)のスレッドが一種の「スレッドキュー」で待機することにほとんどの時間を費やす場合、大きな利点は見られません:)
私の2セント:スレッドの優先順位で遊ぶ代わりに、おそらく、ロードするファイル/画像のキューと、画像を次々にデキューしてロードするスレッドを持つようにコードを再設計することを検討する必要があります。(古典的なコンシューマー/プロデューサーのシナリオ)物事をスピードアップする必要がある場合は、別のスレッドにプリフェッチを実行させると思うかもしれません(swの設計/アーキテクチャで意味がある場合)

チャオチャオ
セルジオ

于 2012-03-25T10:26:06.957 に答える
0

Sergio の言うとおりです。GCD とシリアライズされたキューを確認できれば、それに移行するのはそれほど難しくありません。適切なハウツー手順については、Appleのビデオをご覧ください。

于 2012-03-25T10:46:20.773 に答える