Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing a UIImage inside a OCUnit test target

I'm currently writing an image manipulation test for an iPad app. I have a resources folder inside my unit test target with a photo inside, however when I try to access it using [UIImage imageNamed:@"photo1.jpg"] no image gets returned. If I change the file name to one in the main Resources folder an image does get returned.

Is there a way to access the Resources folder inside the unit test target?

like image 921
treeba Avatar asked Dec 22 '11 10:12

treeba


2 Answers

Found the answer to this, looks like you can't use [UIImage imageNamed:], you can access the image like this:

NSBundle *bundle = [NSBundle bundleForClass:[self class]];
NSString *imagePath = [bundle pathForResource:@"photo1" ofType:@"jpg"];
UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
like image 89
treeba Avatar answered Oct 01 '22 20:10

treeba


Since iOS 8 we have the: -imageNamed:inBundle:compatibleWithTraitCollection: failable init on UIImage

In Swift:

let bundle = NSBundle(forClass: self.dynamicType)
let image:UIImage? = UIImage(named: "imageFileName",
                          inBundle:bundle,
     compatibleWithTraitCollection:nil)

In Objective-C

NSBundle* bundle = [NSBundle bundleForClass:[self class]];
UIImage* image = [UIImage imageNamed:@"imageFileName.extension"
                             inBundle:bundle
        compatibleWithTraitCollection:nil];

Documentation

like image 31
Joseph Lord Avatar answered Oct 01 '22 20:10

Joseph Lord