简体   繁体   中英

How do I determine the path of the file that -[UIImage imageNamed:] will load?

I've got an inherited project with a bunch of code that uses -[UIImage imageWithContentsOfFile:] and a full path. I'm converting it to use -[UIImage imageNamed:] and just the file name (no extension) so I can pass it things like @"icon" and get either icon.png or icon@2x.png or icon~ipad.png , as appropriate.

The problem is, there's a part in the program where I want to check the size of the image and, if it's too big, I want to display, instead, TooBigImage.png .

So I need to know, if I call [UIImage imageNamed: someName] , which extended/modified name it's going to use. Basically, I want the path to that file, so I can check it's size before loading the image.

Alternately, if there's a way to check imageSizeForImageNamed: or something similar, I'm ok using that, I just don't know of any.

I'd rather NOT re-implement the whole "if retina, append @2x, etc..." thing, as that's (a) cumbersome and (b) fragile (what if Apple changes/augments the behaviour?)

Hints?

Thanks!

For pixels use size and scale properties:

UIImage *getMySize = [UIImage imageNamed:@"blah"];

float width = getMySize.scale * getMySize.size.width;

float height = getMySize.scale * getMySize.size.height;

Here is the UIImage Documentation .

As far as I know you should implement your own function to check the size of the file manually.

You can generate the names by yourself like:

-(bool)fileOk:(NSString*)filename
{
    bool retina = [UIScreen mainScreen].scale == 2.0;
    static bool iPad = UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad;

    //Generate the filename
    NSString *fullFilename = [NSString stringWithFormat:@"%@%@%@",filename,(retina)?@"@2x":@"",(iPad)?@"~ipad":@"~iphone"]; 

    //Get the size:

    NSError *error;
    NSString *fullPath=[[NSBundle mainBundle] pathForResource:fullFilename ofType:@"png"];
    NSDictionary *fileDictionary = [[NSFileManager defaultManager] attributesOfItemAtPath: fullPath error:&error];

    return (fileDictionary && [fileDictionary fileSize]<SOME_TO_BIG_SIZE_CONSTANT);
}

Then you can choose if you want to show the image or not. I have not tried the code so there may be some typo... I hope that this is what you where looking for...

[UIImage imageNamed: @"icon"] always looks in the applications main bundle. So icon.png must be located right in the bundle to be found. No subpath is allowed there.

However, you can defined your own bundles by using [NSBundle bundleWithPath: @"subfolder"] the advantage here is that you can then use the bundle methods to retrieve optimized assets.

NSBundle *bundle = [[NSBundle bundleWithPath: @"folder"] autorelease];

Then, [bundle pathForResource:ofType:] will return the correct image resource path from your folder (ie icon~ipad) and [UIImage imageWithContentsOfFile:] will take care of the size modifier.

Although this question does not have an answer it summarizes my experiences with this quit well. How do NSBundle pathForResource:ofType: and UIImage imageWithContentsOfFile: handle scale and device modifiers?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM