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

时间:2020-06-30 21:01:10

标签: ios objective-c xcode uiimageview

目前,我有一个图像文件夹,其路径为“资源/图像”,并且在图像文件夹中有多个图像,我想使用相同的图像名称创建暗模式,但是该图像在暗模式样式和常规样式下有所不同。例如(暗模式下的图标为白色,而普通模式下的图标为黑色)

我想知道是否在Resource / images / imageOneFolder和Resource / images / imageTwoFolder内创建2个文件夹,如何访问特定路径以获取图像。

谢谢

1 个答案:

答案 0 :(得分:0)

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

  • 资源/图像/黑色/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