如何获取UIImage的图像作为NSString? [英] How to get a UIImage's image as an NSString?

查看:54
本文介绍了如何获取UIImage的图像作为NSString?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要以字符串的形式获取UIImage图像的值。

I need to get the value of a UIImage's image as a string.

以前我是在做的:

if(images[imageNo].image == [UIImage imageNamed:@"image1.png"])
{

但是现在我有更多条件,因此我想使用开关来做:

But now I have a lot more conditions so I would like to use a switch, doing:

switch(images[imageNo].image)
{
    case @"image1.png":
    break;
}

有没有办法实现这一目标?谢谢!

Is there a way to achieve this? Thanks!

推荐答案

您的两种方法都不正确。第一个依赖于缓存(未记录),而第二个依赖于语法错误-'case'关键字需要编译时常量表达式。

Both of your approaches are incorrect. The first one relies on caching (which is not documented), meanwhile the second is a syntax error - the 'case' keywords expect a compile-time constant expression.

据我所知,iOS SDK中没有这样的方法可以返回图像的文件名-仅仅是因为它不是图像的属性(以编程方式创建的图像会返回什么?)。相反,您应该尝试在NSDictionary上进行操作,将与文件名关联的UIImage对象存储为键,并使用 isEqualToString:比较键。

As far as I know, there's no such a method in the iOS SDK that would return an image's filename - simply because it's not a property of the image (what would it return for a programmatically created image?). You should instead try to operate on an NSDictionary, storing the UIImage objects associated with the filenames as keys, and comparing the keys using isEqualToString:.

因此,您应该只创建一次图像,并将它们存储在字典中:

So you should create your images only once, and store them in a dictionary:

// assuming `images` is an already created instance variable of your class
[images setObject:[UIImage imageNamed:@"ImgOne.png"] forKey:@"ImgOne.png"];
// and so on, with every image you need

// then once you have to check against a file name, use:
UIImage *img = [images objectForKey:@"FileName.png"];
if ([someImage isEqual:img]) {
    // you can now be sure that the image set to the object was once named "FileName"
}

这篇关于如何获取UIImage的图像作为NSString?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆