从文档目录为UI按钮设置图像

时间:2015-02-04 16:24:19

标签: ios objective-c

我尝试从文档目录

设置UIButton的图像

我试过这个:

-(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);}

日志返回: imageBouton1 =的/ var /移动/应用/ 164F9A40-10AF-402E-A46C-73084CAA8627 /文档/ btn01.png

我也试过这个:

-(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);}

日志返回相同: imageBouton1 =的/ var /移动/应用/ 164F9A40-10AF-402E-A46C-73084CAA8627 /文档/ btn01.png

我的按钮无法加载我的图片 谢谢你的帮助

1 个答案:

答案 0 :(得分:1)

imageNamed:用于捆绑中的文件。你应该使用

NSData *imgData = [[NSData alloc] initWithContentsOfURL:[NSURL fileURLWithPath: imageBouton1]];
[boutonVideo1 setImage:[[UIImage alloc] initWithData:pngfile] forState:UIControlStateNormal];