我目前正在为iPad应用程序编写图像操作测试。我的单元测试目标中有一个资源文件夹,里面有一张照片,但是当我尝试使用[UIImage imageNamed:@“photo1.jpg”]访问它时,没有图像被返回。如果我在主资源文件夹中将文件名更改为一个,则图像将返回。访问OCUnit测试目标内的UIImage
有没有办法访问单元测试目标中的Resources文件夹?
我目前正在为iPad应用程序编写图像操作测试。我的单元测试目标中有一个资源文件夹,里面有一张照片,但是当我尝试使用[UIImage imageNamed:@“photo1.jpg”]访问它时,没有图像被返回。如果我在主资源文件夹中将文件名更改为一个,则图像将返回。访问OCUnit测试目标内的UIImage
有没有办法访问单元测试目标中的Resources文件夹?
找到这个问题的答案,看起来你无法使用[UIImage的imageNamed:],您可以访问这样的形象:
NSBundle *bundle = [NSBundle bundleForClass:[self class]];
NSString *imagePath = [bundle pathForResource:@"photo1" ofType:@"jpg"];
UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
您可以使用以下类别(但仅添加测试目标!)。在测试目标自动方法工作:它将使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];
}
。
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
由于iOS 8的我们有:上UIImage
-imageNamed:inBundle:compatibleWithTraitCollection:
方法在夫特:
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];
这个答案应该在顶部。我已经为UIImage创建了一个新的初始化工具来完成这个工作。我现在感觉有些笨拙。 https://gist.github.com/Psidium/6d15525d4fba7b191290 – 2015-04-23 17:12:48
谢谢,这解决了我的问题。 – 2015-05-06 22:35:48