Reputation: 6863
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?
Upvotes: 0
Views: 742
Reputation: 5432
Use [UIImage imageWithContentsOfFile:[self localImagePath:NO]]
, instead of [UIImage imageNamed:...]
for the one you found using your localImagePath method.
Upvotes: 0
Reputation: 10164
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.
Upvotes: 1
Reputation: 8990
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.
Upvotes: 4