garethdn
garethdn

Reputation: 12373

How to ignore @2x images

I have the following code below to populate an array with images:

NSString *fileName;
    myArray = [[NSMutableArray alloc] init];
    for(int i = 1; i < 285; i++) {
        fileName = [NSString stringWithFormat:@"Animation HD1.2 png sequence/HD1.2_%d.png", i];
        [myArray addObject:[UIImage imageNamed:fileName]];
        NSLog(@"Loaded image: %d", i);
    }

In my resources folder i have @2x versions of each of these images. Is there a way (programmatically) that I can ignore the @2x images on retina devices and populate the array with the non-@2x images?

EDIT 1:

I've edited my code to use NSData:

  myArray = [[NSMutableArray alloc] init];
    for(int i = 1; i < 285; i++) {
        fileName = [NSString stringWithFormat:@"Animation HD1.2 png sequence/HD1.2_%d.png", i];
        NSData *fileData = [NSData dataWithContentsOfFile:fileName];
        UIImage *regularImage = [UIImage imageWithData:fileData];
        [myArray addObject:regularImage];
    }

falling.animationImages = MYArray;

This is crashing my app with the error: *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'. Am i using the NSData object wrong?

Upvotes: 0

Views: 240

Answers (3)

ikuramedia
ikuramedia

Reputation: 6058

I think the NSData technique should work, but you need to get the path from your bundle, you can't just give the filename as a string like that.

Try:

filename = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"Animation HD1.2 png sequence/HD1.2_%d.png", i] ofType:nil];

Upvotes: 1

M&#225;rio Carvalho
M&#225;rio Carvalho

Reputation: 3125

Try this...

//This string will have @"@2x.png"

NSString *verificationString = [myString substringFromIndex:[myString length] - 7];
if(![verificationString isEqualToString:@"@2x.png"]) 
{
    //NOT EQUAL...
}

Upvotes: 0

Steven Fisher
Steven Fisher

Reputation: 44886

I believe the question amounts to "How do I bypass the automatic @2x image loading?"

You need to take a path it can't follow. You could pass the contents of each file using NSData dataWithContentsOfFile:options:error: to UIImage imageWithData: This way, imageWithData won't know where the data came from, let alone that there's a 2x version.

Upvotes: 3

Related Questions