使用apportable将iOS应用程序移植到Android时,如果已经本地化(不同语言的不同版本),则无法访问应用程序内的资源(它们无法在运行时加载):
通过Xcode将本地化图像添加到项目中(不同语言的图像的不同版本) - apportable会将这些图像作为特定文件夹下的资源加载到.apk包中。例如:
assets/en.lproj/image.png (generic english version) assets/zh-Hans.lproj/image.png (simplified chinese version) assets/zh-Hant.lproj/image.png (traditional chinese version)
尝试加载图片:
UIImage *image = [UIImage imageNamed:imageName]; // This works under iOS but not under apportable
如果图像已如上所述进行了本地化,则返回nil。
答案 0 :(得分:0)
以下是一个有效的临时解决方法:
Utils.h:
#if defined(ANDROID) && defined(WORKAROUND_APPORTABLE_LOCALIZED_IMAGES)
#define LOCALIZEDIMAGE LocalizedUIImage
@interface LocalizedUIImage : UIImage
+(UIImage *)imageNamed:(NSString *)imageName;
@end
#else
#define LOCALIZEDIMAGE UIImage
#endif
Utils.m:
#if defined(ANDROID) && defined(WORKAROUND_APPORTABLE_LOCALIZED_IMAGES)
#import <Foundation/Foundation.h>
@implementation LocalizedUIImage
+(UIImage *)imageNamed:(NSString *)imageName {
UIImage *image = nil;
if ([imageName rangeOfString:@"/"].location == NSNotFound) {
NSLocale *locale = [NSLocale currentLocale];
NSString *localeId = [locale localeIdentifier];
NSString *localizedImageName = [[localeId stringByAppendingString:@".lproj/"] stringByAppendingString:imageName];
image = [UIImage imageNamed:localizedImageName];
if (image == nil) { // Retry for a generic language version (ie: en rather than en_US )
NSUInteger hyphenatedLocale = [localeId rangeOfString:@"_"].location;
if (hyphenatedLocale != NSNotFound) {
NSString *localizedImageName = [[[localeId substringToIndex:hyphenatedLocale] stringByAppendingString:@".lproj/"] stringByAppendingString:imageName];
image = [UIImage imageNamed:localizedImageName];
}
}
}
if (image == nil)
image = [UIImage imageNamed:imageName];
return image;
}
@end
#endif
使用者:
UIImage *image = [LOCALIZEDIMAGE imageNamed:imageName];