Creating and Initializing Image Objects with file contents

suggest change

imageWithContentsOfFile: loads image data from a path. cellCountry is a dictionary, and Country_Flag is the resource name (with its extension). pathForResource:ofType: returns nil when that file is not in the main bundle, and then imageWithContentsOfFile: returns nil.

Example:

NSString *flagName = [cellCountry objectForKey:@"Country_Flag"];
NSString *flagPath = [[NSBundle mainBundle] pathForResource:flagName ofType:nil];
UIImage *image = [UIImage imageWithContentsOfFile:flagPath];

Using an array of numbered images (photo1.jpg, photo2.jpg, and so on when myPhoto is photo):

NSMutableArray *imageArray = [[NSMutableArray alloc] init];

for (int imageNumber = 1; self.myPhoto != nil; imageNumber++) {
   NSString *fileName = [NSString stringWithFormat:@"%@%d.jpg", self.myPhoto, imageNumber];
   NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:nil];
   UIImage *image = [UIImage imageWithContentsOfFile:path];

   if (image) {
       [imageArray addObject:image];
   } else {
       break;
   }
}

//Using image array for animations here
self.myImageView.animationImages = imageArray;

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents