2

I have this piece of code I use to test animations in iOS. The goal is to load 4 png's and animate them in an UIImageView. I have an NSMutableArray imageNames in which I keep the paths to the images. It gets filled correctly. Then when I try to use it to create UIImage it works the first time, but it returns nil afterwards. Why does this happen? I can't wrap my head around this.

for (NSInteger i = 0; i < imageNames.count; i++) { NSString *name = [imageNames objectAtIndex:i]; [images addObject:[UIImage imageNamed:name]]; } 

Here's the full code:

- (void)viewDidLoad { [super viewDidLoad]; UIView *test = [[UIView alloc] initWithFrame:CGRectMake(200, 200, 50, 50)]; test.backgroundColor = [UIColor purpleColor]; [self.view addSubview:test]; self.view.backgroundColor = [UIColor greenColor]; NSMutableArray *imageNames = [[NSMutableArray alloc] initWithCapacity:4]; NSString *basePath = @"/Users/johndoe/Desktop/soa/SoA/SoA/Models/firefly/"; // Load images for (NSInteger i = 0; i < 4; i++) { NSMutableString *fullPath = [NSMutableString stringWithString:basePath]; [fullPath appendFormat:@"SMALL_0000_Capa-%ld.png", i + 1]; [imageNames addObject:fullPath]; // [fullPath appendString:@"SMALL_0000_Capa-1.png"]; } NSMutableArray *images = [[NSMutableArray alloc] init]; for (NSInteger i = 0; i < imageNames.count; i++) { NSString *name = [imageNames objectAtIndex:i]; [images addObject:[UIImage imageNamed:name]]; } // Normal Animation UIImageView *animationImageView = [[UIImageView alloc] initWithFrame:CGRectMake(60, 95, 86, 193)]; animationImageView.animationImages = images; animationImageView.animationDuration = 0.5; [self.view addSubview:animationImageView]; [animationImageView startAnimating]; } 
2
  • 4
    The UIImage imageNamed: only takes a base filename - no path, no extension. The named image should be an image found in your app's resource bundle. Why are you passing a full path? Why are you hardcoding a path on your computer for an iOS app? All of this is wrong. Commented Feb 3, 2016 at 19:08
  • I would imagine it's wrong, but it's for testing purposes, since I don't have any experience with iOS. So for now I'm just trying to show something on the screen. :D Commented Feb 3, 2016 at 19:28

2 Answers 2

1

Well, as you said that it's for learning purposes I've tried your code and the only real problem I found is the one pointed by @rmaddy. I don't know where your image is stored exactly but I tried it adding 4 images as Assets and changed your code from this:

NSString *basePath = @"/Users/johndoe/Desktop/soa/SoA/SoA/Models/firefly/"; // Load images for (NSInteger i = 0; i < 4; i++) { NSMutableString *fullPath = [NSMutableString stringWithString:basePath]; [fullPath appendFormat:@"SMALL_0000_Capa-%ld.png", i + 1]; [imageNames addObject:fullPath]; } 

to this:

// Load images for (NSInteger i = 0; i < 4; i++) { NSString *path = [NSString stringWithFormat:@"pos%i", i]; [imageNames addObject:path]; } 

and I think it worked as you expected.

Sign up to request clarification or add additional context in comments.

2 Comments

I found my mistake. It was very stupid :D. I didn't see that my images had different names. I chose your answer as best, for telling me to use assets, which i didn't know nothing about.
If those images will be static (will be provided with your app) then I would recommend those to be in Assets. Another approach, if you will download the images from somewhere over the internet, is to save the image in some of your app's directory (like Document) and load it from there. You can read more from here: Working with Directories on iOS
0

Try to replace your

animationImageView.animationImages = images; 

by this method:

[animationImageView setAnimationImagesWithUIImageViewSize:images]; 

This solution is provided by using this category: UIImageView+AnimationImages.m

2 Comments

I didn't test this solution yet. :]
Ok, I will try this, but nevertheless it throws error when I try to add new image in images: 'object cannot be nil'. So i don't even get there in the first place. The interesting thing is that the new image is null when i = 1, but not when i = 0, from where my confusion arose.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.