简体   繁体   中英

IPhone UIImageView Image

I am trying to set image to UIImageView programmatically but it does't work. I have following code.

[image setImage:[UIImage imageNamed:[self localImagePath:NO]]];

and

-(NSString *)localImagePath:(BOOL)forSave {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSPicturesDirectory, NSUserDomainMask, YES);
    NSString *picturesDirectory = [paths objectAtIndex:0];
    NSString *picturesPath = [picturesDirectory stringByAppendingPathComponent:@"image.png"];
    if (forSave || [[NSFileManager defaultManager] fileExistsAtPath:picturesPath]) {
        return picturesPath;
    }
    else {
        return [[NSBundle mainBundle] pathForResource:@"image" ofType:@"png"];
    }
}

Is it possible to set image to UIImageView on run time from different mediums?

The imageNamed: method takes only the filename as an argument, not a path. Also it will look for that file in the main bundle only. Try using imageWithContentsOfFile: instead.

You'll have to use two different ways to load the image

[UIImage imageNamed: name] for the resource based one

[UIImage imageWithContentsOfFile: filename] for the one you found locally.

consider changing your method to return the UIImage and use the appropriate loading method internally.

使用[UIImage imageWithContentsOfFile:[self localImagePath:NO]]代替使用您的localImagePath方法找到的[UIImage imageNamed:...]

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