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?
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:toUIImage imageWithData:This way,imageWithDatawon’t know where the data came from, let alone that there’s a 2x version.