开发者

IPhone UIImageView Image

开发者 https://www.devze.com 2023-02-14 04:24 出处:网络
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]]];

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.


Use [UIImage imageWithContentsOfFile:[self localImagePath:NO]], instead of [UIImage imageNamed:...] for the one you found using your localImagePath method.

0

精彩评论

暂无评论...
验证码 换一张
取 消