3

アプリが読み込まれたときに、画像を配列に遅延読み込みしています。NSMutableArrayとNSArrayを使用してみました(作成後に配列を変更する必要はありません)が、後者がクラッシュします。

...
[self performSelectorInBackground:@selector(loadImageArrays) withObject:nil];
...

- (void)loadImageArrays {

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

    imageArray = [[NSMutableArray alloc] init];
    for(int i = 0; i <= x; i++) {
        fileName = [NSString stringWithFormat:@"image_0000%d.png", i];
        [imageArray addObject:[UIImage imageNamed:fileName]];
    }
    [pool drain];
}

vs

NSAutoreleasePool *pool;
pool = [[NSAutoreleasePool alloc] init];
imageArray = [[NSArray alloc] initWithObjects:
            [UIImage imageNamed:@"image_00000.png"],
            [UIImage imageNamed:@"image_00001.png"],
            [UIImage imageNamed:@"image_0000X.png"],
                    nil];
[pool drain];

NSZombieEnabled = YESは、後者のコードスニペットを使用したときに、[UIImageretain]が割り当て解除されたインスタンスに送信されたことを示します。両方の配列は、私のhファイルに(非アトミック、保持)プロパティを持っています。NSArrayによって画像が保持されないのはなぜですか?

4

2 に答える 2

4

UIImageはUIKitの一部であり、スレッドセーフではありません。たとえば、このメソッドは、クラスがキャッシュに使用imageNamed:するグローバルな名前-画像ディクショナリを破壊する可能性があります。UIImage

バックグラウンドスレッドに画像をロードする場合は、CoreGraphicsを使用する必要があります。

あなたのコメントに答えるために編集してください:

PNGファイルは次のコマンドでロードできます。

CGDataProviderRef source = CGDataProviderCreateWithURL((CFURLRef)url);
CGImageRef image = CGImageCreateWithPNGDataProvider(source, NULL, NO, kCGRenderingIntentDefault);
CFRelease(source);

ACGImageはコアファウンデーションオブジェクトであり、に格納できますNSArray。次に、UIImageそれから(もちろん、メインスレッドで)作成できます。

[[UIImage alloc] initWithCGImage:image]
于 2010-01-26T17:03:26.120 に答える
0

Although I know there is a big difference in mutable and immutable arrays, I'm in doubt myself. Something tells me this isn't purely a mutable vs immutable issue. Looks like your pool is drained prematurely (sounds nasty). Not that it should make a difference, but could you try to spawn a new thread by doing;

[NSThread detachNewThreadSelector:@selector(loadImageArrays) toTarget:self withObject:nil];

I'm simply curious of the result.. Also try both snippets in a clean environment (i.e: with no other code around). If your second code snippet works there, you should be looking elsewhere for the solution.

于 2010-01-26T16:57:06.140 に答える