dynamically get the image from different resource folder iOS

Steven

Currently I have the image folder the paths is Resource/images and inside the images folder there are several images, I would like to create dark mode using the same image name but the image are different in dark mode style and normal style. example(icon in dark mode is white but in normal mode is black)

I would like to know if I create 2 folder inside Resource/images/imageOneFolder and Resource/images/imageTwoFolder, how can I access the specific path to get the image.

Thanks

Ladd.c

first you need to organize your icons in a better way for example:

  • Resources/images/black/myicon_black.png
  • Resources/images/white/myicon_white.png

Having this level of organization resolve the problem is very easy, I provide you two ways to resolve using the next logic:

@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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to get image from assets by folder ios

Get the resource folder path of an image

Dynamically get image paths in folder with Nuxt

How to get file from resource folder as File?

Get a string resource from "values" folder in Android

How to Get Image From Image Resource Id

Store an image from gallery to a different folder

How to get Image from resource project

Php: get image dimensions from file or resource

How to get IRandomAccessStreamReference from image resource?

Spring Boot unabe to serve static image from resource folder

image not loading in javafx css from resource folder while making jar

how to load image from java project's resource/images folder?

Dynamically get resource object

Change background image dynamically from get response

How to make two different folder get resources from one folder?

Get image from a external folder - JAVA - JSF

Get image from resources folder - Laravel

SSIS Get Image from Shared Folder

How to get and save image to folder from listview

How to load image from resource folder and set it to source image of UI Image in unity5

How can i get images dynamically from drawable folder?

kotlin - get image from image array in drawable folder

How to get Image Resource

How can I get og:image from resource with VBA excel

Can't get Python to import from a different folder

SSIS to get the particular file from multiple files and copy to different folder

Save An Image To Application Documents Folder From UIView On IOS

React Native iOS read image from apps Documents folder

TOP Ranking

HotTag

Archive