【问题标题】:set an Image for a UIbutton from documents directory从文档目录为 UIbutton 设置图像
【发布时间】:2015-04-04 06:25:01
【问题描述】:

我尝试从文档目录中为 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

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

【问题讨论】:

  • 您是否检查过路径是否与保存图像的路径相同?图片扩展名正确吗?
  • 是的,我检查了路径是否正确,我用 ifunbox 打开了我的应用程序,我的图像在 Documents 目录中 -:((
  • 然后,您可以尝试将NSFileManager 用作[[NSFileManager defaultManager] contentsAtPath:pngfile],它会返回一个NSData,因此您可以使用此数据初始化一个UIImage

标签: ios objective-c


【解决方案1】:

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

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

【讨论】:

  • 对不起,我刚刚更新了我的答案,应该是 [NSURL fileURLWithPath: imageBouton1]
  • @ dminones 仍然无法使用: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];
  • 您确认该图像存在于该路径吗?
  • 是的,我用 ifunbox 打开我的应用程序,我的文件在我的 Documents 目录中(我用 iTunes 将它放在我的应用程序的共享文件目录中)
  • 使用 type :custom 可以正常工作!!!是的!非常感谢我的朋友....为你 +1
猜你喜欢
  • 2016-06-30
  • 2012-01-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-03
  • 2023-03-25
  • 2015-05-27
  • 1970-01-01
相关资源
最近更新 更多