提问人:Steven 提问时间:7/1/2020 更新时间:7/1/2020 访问量:300
从不同的资源文件夹 iOS 动态获取图像
dynamically get the image from different resource folder iOS
问:
目前我有图像文件夹,路径是 Resource/images,在图像文件夹中有几张图像,我想使用相同的图像名称创建深色模式,但图像在深色模式样式和正常样式中有所不同。示例(深色模式下的图标为白色,但正常模式下的图标为黑色)
我想知道如果我在 Resource/images/imageOneFolder 和 Resource/images/imageTwoFolder 中创建 2 个文件夹,我该如何访问获取图像的特定路径。
谢谢
答:
0赞
Ladd.c
7/1/2020
#1
首先,您需要以更好的方式组织图标,例如:
- 资源/图片/黑色/myicon_black.png
- 资源/图片/白色/myicon_white.png
让这个级别的组织解决问题非常容易,我为您提供了两种使用下一个逻辑解决问题的方法:
@interface MyStructObject : NSObject
@property (nonatomic) NSString *imageName, *imageExtension, *fullImageName;
@end
@interface MyViewController : UIViewController
@end
@implementation MyViewController
// For this short version your icons needs to be white or black
// With the next code you can change the color of your icon
// Example: If dark mode active you need icons with whiteColor
// if not dark mode you need icons with blackColor
- (void)didExecuteShortVersion
{
// Initial path document or bundle
NSString *resourcesPath = @"Resources/images";
// Getting objects from array or your own struct
MyStructObject *myItem = [MyStructObject new];
// The image full path
NSString *fullImagePath = [NSString stringWithFormat: @"%@/%@", resourcesPath, myItem.fullImageName];
// Image Object
UIImage *imageIcon = [UIImage imageWithContentsOfFile: fullImagePath];
// Image Container
UIImageView *myImageView = [[UIImageView alloc] initWithFrame: CGRectZero];
[myImageView setImage: imageIcon];
myImageView.image = [myImageView.image imageWithRenderingMode: UIImageRenderingModeAlwaysTemplate];
if (@available(iOS 12.0, *)) {
if (self.traitCollection.userInterfaceStyle == UIUserInterfaceStyleDark) {
[myImageView setTintColor: [UIColor whiteColor]];
}
else {
[myImageView setTintColor: [UIColor blackColor]];
}
}
else {
// Dark mode is not available
[myImageView setTintColor: [UIColor blackColor]];
}
// Adding view
[self.view addSubview: myImageView];
}
#pragma mark - Life Cycle
- (void)viewDidLoad
{
// Initial path document or bundle
NSString *resourcesPath = @"Resources/images";
// The image full path
NSString *fullImagePath = nil;
// Getting objects from array or your own struct
MyStructObject *myItem = [MyStructObject new];
if (@available(iOS 12.0, *)) {
if (self.traitCollection.userInterfaceStyle == UIUserInterfaceStyleDark) {
NSLog(@"Dark Mode Active");
// Assuming your struct only has the first name of icon
// Example: myItem.imageName -> myicon
// Adding the last path of name to match icon _black -> myicon_black
// If you have different kind of extension you can improve
// the logic of this
fullImagePath = [NSString stringWithFormat: @"%@/black/%@_black.%@", resourcesPath, myItem.imageName, myItem.imageExtension];
}
else {
NSLog(@"White Mode Active");
fullImagePath = [NSString stringWithFormat: @"%@/white/%@_white.%@", resourcesPath, myItem.imageName, myItem.imageExtension];
}
} else {
// Dark mode is not available
fullImagePath = [NSString stringWithFormat: @"%@/default/%@_default.%@", resourcesPath, myItem.imageName, myItem.imageExtension];
}
if (fullImagePath != nil) {
UIImage *imageIcon = [UIImage imageWithContentsOfFile: fullImagePath];
// Change the CGRect
UIImageView *myImageView = [[UIImageView alloc] initWithFrame: CGRectZero];
[myImageView setImage: imageIcon];
// Add myImageView to the UIView
[self.view addSubview: myImageView];
}
}
@end
评论
0赞
Steven
7/3/2020
感谢您抽出宝贵时间接受采访。
评论