ios - 从文档目录为 UIbutton 设置图像

标签 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/mobile/Applications/164F9A40-10AF-402E-A46C-73084CAA8627/Documents/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/mobile/Applications/164F9A40-10AF-402E-A46C-73084CAA8627/Documents/btn01.png

我的按钮没有加载我的图片 谢谢你的帮助

最佳答案

imageNamed:用于您 bundle 中的文件。你应该使用

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

关于ios - 从文档目录为 UIbutton 设置图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28326332/

相关文章:

ios - watchos2 - 我可以实现多个 Storyboard吗?

ios - 按按钮打开 Safari,如何返回我的应用程序

iphone - 在多个 iOS 设备上同时触发事件

ios - CoreData 和线程安全

ios - 两台苹果电脑。一份 iPhone 开发者许可证。可能的?

ios - UITableViewCell,显示没有滑动的滑动式删除按钮

iphone - NSMutableArray具有相同的引用

ios - 方法 load() 定义了 Objective-C 类方法 'load' ,这是 Swift 1.2 不允许的

ios - IOS程序中如何使用equalTo方法

objective-c - 获取所有对象属性的列表