Loading images into NSArray using initWithObjects crashes, but not NSMutableArray?

I am doing lazy loading images into an array when the application has loaded. I tried using NSMutableArray and NSArray (I don't need to modify the array after creating it), but the latter falls on me.

...
[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 tells me that [UIImage save] was sent to the freed instance when using the last code snippet. Both arrays have a (non-atomic, persistent) property in my h file. Why are images not saved by NSArray?

+3
source share
2 answers

UIImage UIKit, . , imageNamed: , UIImage .

Core Graphics, .

, :

PNG

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

A CGImage NSArray. UIImage ( , ):

[[UIImage alloc] initWithCGImage:image]
+3

, , . - , . , ( ). , , , :

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

. (.. - ). , .

0

Source: https://habr.com/ru/post/1730075/


All Articles