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?
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];
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With