iphone - 如何显示不带扩展名的图像和图像名称

标签 iphone ios uiimageview nsmutablearray

在我看来,我有一个 UIimageview 和 4 个 UIButton ,我显示来自 NSMutableArray 的图像。如何在没有 .png 的情况下将 UIButton 标题显示为 imageview 图像名称?我尝试使用以下编码

imageary=[[NSMutableArray alloc]initWithObjects:[UIImage imageNamed:@"dear.jpg"],[UIImage imageNamed:@"donkey.jpg"],[UIImage imageNamed:@"elephant.jpg"],[UIImage imageNamed:@"fox.jpg"],[UIImage imageNamed:@"giraffe.jpg"],[UIImage imageNamed:@"goat.jpg"],[UIImage imageNamed:@"buffallo.jpg"],[UIImage imageNamed:@"bull.jpg"],[UIImage imageNamed:@"cow.jpg"],[UIImage imageNamed:@"crocodile.jpg"], nil];


 - (NSString *) convertToDisplayName:(NSString *)actual
{
return [actual stringByReplacingOccurrencesOfString:@".png" withString:@" "];

}

尝试使用上述方法检索名称时

int i=0;
for(UIView *view in self.view.subviews)
{
    if([view isKindOfClass:[UIButton class]])
    {
       mybutton = (UIButton *)view;
        if(mybutton.tag == 1||mybutton.tag == 2||mybutton.tag == 3||mybutton.tag == 4)
        {

             animage.image=[imageary objectAtIndex:i];
            name=[self convertToDisplayName:[imageary objectAtIndex:i]];
            [mybutton setTitle:name forState:UIControlStateNormal];
            NSLog(@"current image name :%@",name);
            i++;

        }
    }
   }

显示错误

 Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIImage stringByReplacingOccurrencesOfString:withString:]: unrecognized selector sent to instance 0x73f8eb0

请帮助我纠正错误。

最佳答案

您正在该函数中传递 UIImage 对象,您应该维护图像名称数组并在此函数中传递该图像名称,顺便说一句,您可以通过使用此方法删除扩展名来简单地获取名称:

[actual stringByDeletingPathExtension];

这是你应该做的

imageary=[[NSMutableArray alloc]initWithObjects:@"dear.jpg",@"donkey.jpg",@"elephant.jpg",@"fox.jpg",@"giraffe.jpg",@"goat.jpg",@"buffallo.jpg",@"bull.jpg",@"cow.jpg",@"crocodile.jpg", nil]; // declare array of names not images
 - (NSString *) convertToDisplayName:(NSString *)actual
{
  return [actual stringByDeletingPathExtension]; //return image name without extension
}

比这个

int i=0;
for(UIView *view in self.view.subviews)
{
if([view isKindOfClass:[UIButton class]])
{
   mybutton = (UIButton *)view;
    if(mybutton.tag == 1||mybutton.tag == 2||mybutton.tag == 3||mybutton.tag == 4)
    {
         i = rand() % [imageary count]; // set random image
         animage.image=[UIImage imageNamed:[imageary objectAtIndex:i]]; // return image with this name
        name=[self convertToDisplayName:[imageary objectAtIndex:i]];
        [mybutton setTitle:name forState:UIControlStateNormal];
        NSLog(@"current image name :%@",name);
        i++;

    }
}
}

希望有帮助!

关于iphone - 如何显示不带扩展名的图像和图像名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13282472/

相关文章:

ios - Flutter iOS 版本和配置文件构建无法正常工作

ios - Swift 3 设备运动重力

iphone - 现有应用程序的更新

iphone - 将 UIImage 传递给第二个 View Controller 会延迟转换

ios - 如何在 Xcode Storyboard中设置正确的 UIImageView?

iphone - 更改编辑器iTunes应用商店的名称

ios - 在本地下载并构建迦太基,以避免加入CI

iphone - 每次选择选项卡时如何加载我的 Root View ?

ios - 如何从数组元素中删除括号

ios - 如何基于UIImageView高度设置不同的UITableViewCell高度?