访问OCUnit测试目标中的UIImage

时间:2011-12-22 10:59:50

标签: iphone objective-c ipad unit-testing ocunit

我正在为iPad应用程序编写图像处理测试。我的单元测试目标中有一个资源文件夹,里面有一张照片,但是当我尝试使用[UIImage imageNamed:@“photo1.jpg”]访问它时,没有返回任何图像。如果我在主资源文件夹中将文件名更改为1,则会返回图像。

有没有办法访问单元测试目标中的Resources文件夹?

4 个答案:

答案 0 :(得分:23)

找到答案,看起来你不能使用[UIImage imageNamed:],你可以像这样访问图像:

NSBundle *bundle = [NSBundle bundleForClass:[self class]];
NSString *imagePath = [bundle pathForResource:@"photo1" ofType:@"jpg"];
UIImage *image = [UIImage imageWithContentsOfFile:imagePath];

答案 1 :(得分:12)

自iOS 8起,我们在-imageNamed:inBundle:compatibleWithTraitCollection:

上使用了UIImage方法

在斯威夫特:

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

在Objective-C

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

答案 2 :(得分:3)

为此制作了一个方便的类别。

image = [UIImage testImageNamed:@"image.png"];

如:

@interface BundleLocator : NSObject
@end

@interface UIImage (Test)
+(UIImage*)testImageNamed:(NSString*) imageName;
@end

@implementation BundleLocator
@end

@implementation UIImage (Test)
+(UIImage*)testImageNamed:(NSString*) imageName
{
    NSBundle *bundle = [NSBundle bundleForClass:[BundleLocator class]];
    NSString *imagePath = [bundle pathForResource:imageName.stringByDeletingPathExtension ofType:imageName.pathExtension];
    return [UIImage imageWithContentsOfFile:imagePath];
}
@end

答案 3 :(得分:2)

您可以使用以下类别(但仅将其添加到测试目标!)。它将使UIImage imageNamed:方法在测试目标中自动工作:

.h文件

/**
 * UIImage imageNamed: method does not work in unit test
 * This category provides workaround that works just like imageNamed method from UIImage class.
 * Works only for png files. If you need other extension, use imageNamed:extension: method.
 * NOTE: Do not load this category or use methods defined in it in targets other than unit tests
 * as it will replace original imageNamed: method from UIImage class!
 */
@interface UIImage (ImageNamedForTests)

/**
 * Returns image with the specified name and extension.
 * @param imageName Name of the image file. Should not contain extension.
 * NOTE: You do not have to specify '@2x' in the filename for retina files - it is done automatically.
 * @param extension Extension for the image file. Should not contain dot.
 * @return UIImage instance or nil if the image with specified name and extension can not be found.
 */
+ (UIImage *)imageNamed:(NSString *)imageName extension:(NSString *)extension;


@end

.m文件

#import "UIImage+ImageNamedForTests.h"
#import <objc/runtime.h>

@implementation UIImage (ImageNamedForTests)

+ (void)load {
    [self swizzleClassMethod];
}

+ (void)swizzleClassMethod {
    SEL originalSelector = @selector(imageNamed:);
    SEL newSelector = @selector(swizzled_imageNamed:);
    Method origMethod = class_getClassMethod([UIImage class], originalSelector);
    Method newMethod = class_getClassMethod([UIImage class], newSelector);
    Class class = object_getClass([UIImage class]);
    if (class_addMethod(class, originalSelector, method_getImplementation(newMethod), method_getTypeEncoding(newMethod))) {
        class_replaceMethod(class, newSelector, method_getImplementation(origMethod), method_getTypeEncoding(origMethod));
    } else {
        method_exchangeImplementations(origMethod, newMethod);
    }
}

+ (UIImage *)swizzled_imageNamed:(NSString *)imageName {
    return [self imageNamed:imageName extension:@"png"];
}

+ (UIImage *)imageNamed:(NSString *)imageName extension:(NSString *)extension {
    NSBundle *bundle = [NSBundle bundleForClass:[SomeClassFromUnitTestTarget class]];//Any class from test bundle can be used. NOTE: Do not use UIImage, as it is from different bundle
    NSString *imagePath = [bundle pathForResource:imageName ofType:extension];
    return [UIImage imageWithContentsOfFile:imagePath];
}