0

I try to set an image for a UIButton from documents directory

i tried this:

-(IBAction) vignettesBank1:(id)sender {

NSString *localPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];

NSString *imageBouton1 = [localPath stringByAppendingPathComponent:[[NSString alloc]initWithFormat:@"btn01.png"]];

[boutonVideo1 setImage:[UIImage imageNamed:imageBouton1] forState:UIControlStateNormal];

NSLog(@"imageBouton1=%@",imageBouton1);}

log return : imageBouton1=/var/mobile/Applications/164F9A40-10AF-402E-A46C-73084CAA8627/Documents/btn01.png

i try this too :

-(IBAction) vignettesBank1:(id)sender {

NSArray  *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentsDir  = [documentPaths objectAtIndex:0];

NSString  *pngfile = [documentsDir stringByAppendingPathComponent:@"btn01.png"];

[boutonVideo1 setImage:[UIImage imageNamed:pngfile] forState:UIControlStateNormal];

NSLog(@"imageBouton1=%@",pngfile);}

log return the same : imageBouton1=/var/mobile/Applications/164F9A40-10AF-402E-A46C-73084CAA8627/Documents/btn01.png

my button doesn't load my image thank's for your help

jeff
  • 5
  • 3
  • have you checked if the path it's the same as the path were the image was saved? the image extension is correct? – Javito_009 Feb 04 '15 at 16:33
  • yes i checked the path is correct and i open my app with ifunbox and my image is in the Documents directory -:(( – jeff Feb 04 '15 at 16:36
  • then, you can try with `NSFileManager` as `[[NSFileManager defaultManager] contentsAtPath:pngfile]`, it returns a `NSData` so then you can init an `UIImage` with this data. – Javito_009 Feb 04 '15 at 16:43

1 Answers1

1

imageNamed: is for files in you bundle. You should use

NSData *imgData = [[NSData alloc] initWithContentsOfURL:[NSURL fileURLWithPath: imageBouton1]];
[boutonVideo1 setImage:[[UIImage alloc] initWithData:pngfile] forState:UIControlStateNormal];
dminones
  • 2,236
  • 20
  • 21
  • sorry, i just updated my answer, should be [NSURL fileURLWithPath: imageBouton1] – dminones Feb 04 '15 at 18:53
  • @ dminones still not work with : `NSString *localPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]; NSString *imageBouton1 = [localPath stringByAppendingPathComponent:[[NSString alloc]initWithFormat:@"btn01.png"]]; NSData *imgData = [[NSData alloc] initWithContentsOfURL:[NSURL fileURLWithPath: imageBouton1]]; [boutonVideo1 setImage:[[UIImage alloc] initWithData:imgData] forState:UIControlStateNormal];` – jeff Feb 04 '15 at 19:07
  • did you confirm that this image exists at that path? – dminones Feb 04 '15 at 19:14
  • yes i open my app with ifunbox and my file is inside my Documents directory (i puted it with itunes in the shared file directory of my app) – jeff Feb 04 '15 at 19:18
  • you could try this: http://stackoverflow.com/questions/1638834/how-to-check-if-a-file-exists-in-documents-folder to see if this is true from your app perspective, i would debug this issue with the simulator where is easier see your devices files – dminones Feb 04 '15 at 19:24
  • works fine with --> setBackgroundImage --- stange --- – jeff Feb 04 '15 at 19:29
  • is the button type custom? frame with the right size? – dminones Feb 04 '15 at 19:37
  • you should try with button type custom – dminones Feb 04 '15 at 20:16
  • 1
    work's fine with type :custom !!! YES! thank's a lot my friend .... +1 for you – jeff Feb 04 '15 at 20:23