从不同的资源文件夹动态获取图像 iOS

dynamically get the image from different resource folder iOS

目前我有图片文件夹,路径是Resource/images并且在图片文件夹中有几张图片,我想使用相同的图片名称创建深色模式,但图片在深色模式下的风格不同和正常的风格。示例(深色模式下图标为白色,普通模式下图标为黑色)

我想知道如果我在Resource/images/imageOneFolder 和Resource/images/imageTwoFolder 中创建2 个文件夹,如何访问具体路径来获取图像。

谢谢

首先您需要以更好的方式组织您的图标,例如:

  • Resources/images/black/myicon_black.png
  • Resources/images/white/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