xcode 如何在 iOS 中从 UIImage 的 NSArray 显示图像名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7030810/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to show image name from NSArray of UIImage in iOS
提问by Mohd Shahid
I have an array of images, I just want to get the image name from the array e.g.
我有一组图像,我只想从数组中获取图像名称,例如
countryArray = [[NSMutableArray arrayWithObjects:
[UIImage imageNamed:@"india.png"],
[UIImage imageNamed:@"australia.png"],
[UIImage imageNamed:@"singapore.png"],
[UIImage imageNamed:@"america.png"],
nil] retain];
NSLog(@"image name at row %d is '%@'.",1,[countryArray objectAtIndex:1]);
It should show australia.png
, instead it shows:
它应该显示australia.png
,而是显示:
image name at row 1 is UIImage: 0x4b14910
第 1 行的图像名称是 UIImage: 0x4b14910
回答by PengOne
A UIImage
doesn't remember its filename once the image is loaded, so there is no direct way to achieve this with a property, e.g. myImage.name
.
UIImage
一旦图像被加载,A就不会记住它的文件名,因此没有直接的方法可以通过属性来实现这一点,例如myImage.name
.
One option is to use a separate NSMutableArray
to store the names as strings. For instance
一种选择是使用单独NSMutableArray
的名称将名称存储为字符串。例如
countryStringArray = [[NSMutableArray arrayWithObjects:
@"india",
@"australia",
@"singapore",
@"america",
nil] retain];
countryImageArray = [[NSMutableArray arrayWithObjects:
[UIImage imageNamed:[countryStringArray objectAtIndex:0]],
[UIImage imageNamed:[countryStringArray objectAtIndex:1]],
[UIImage imageNamed:[countryStringArray objectAtIndex:2]],
[UIImage imageNamed:[countryStringArray objectAtIndex:3]],
nil] retain];
You could even make use of a for
loop to populate the countryImageArray
.
您甚至可以使用for
循环来填充countryImageArray
.
A second option is to use an NSMutableDictionary
. This is essentially the same thing, in fact you can achieve it by
第二种选择是使用NSMutableDictionary
. 这本质上是一样的,事实上你可以通过
countryDictionary = [NSDictionary dictionaryWithObjects:countryImageArray forKeys:countryStringArray];
but it has some advantages in terms of cleanness of retrieving a specific country image by name. The direct way to define it is
但它在按名称检索特定国家/地区图像的清洁性方面具有一些优势。定义它的直接方法是
[countryDictionary setObject:[UIImage imageNamed:@"india.png"] forKey:@"india"];
[countryDictionary setObject:[UIImage imageNamed:@"australia.png"] forKey:@"australia"];
[countryDictionary setObject:[UIImage imageNamed:@"singapore.png"] forKey:@"singapore"];
[countryDictionary setObject:[UIImage imageNamed:@"america.png"] forKey:@"america"];