gpt4 book ai didi

iphone - 如何在没有扩展名的情况下显示图像和图像名称

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:13:01 25 4
gpt4 key购买 nike

在我看来,我有一个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com