如何显示没有扩展名的图像和图像名称

时间:2012-11-08 04:35:47

标签: iphone ios uiimageview nsmutablearray

在我看来,我有一个UIimageview和4 UIButton,我会显示来自NSMutableArray的图像。如何在没有UIButton的情况下将.png标题显示为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

请帮助我纠正。

4 个答案:

答案 0 :(得分:3)

您正在该函数中传递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++;

    }
}
}

希望有所帮助!

答案 1 :(得分:0)

在这行代码中:

name=[self convertToDisplayName:[imageary objectAtIndex:i]];

您将UIImage对象传递给需要NSString对象的方法。

您需要传递文件名,而不是图像。

作为旁注 - 这一行:

return [actual stringByReplacingOccurrencesOfString:@".png" withString:@" "];

应该是:

return [actual stringByReplacingOccurrencesOfString:@".png" withString:@""];

答案 2 :(得分:0)

看起来好像

  

[imageary objectAtIndex:i]

  

的UIImage

对象。它不是NSString。所以你得到了例外。

你可以使用图像名称制作图像,你可以这样做

  

animage.image = [UIImage imageNamed:[imageary objectAtIndex:i]];

答案 3 :(得分:0)

您可以使用: NSArray *arr = [[imageary objectAtIndex:indexNo] componentsSeparatedByString:@"."]; Nsstring *imageName = [arr objectAtIndex:0];

您可以使用imageName设置您的UIButton标题