如何使用字符串参数为 iPhone 表格单元格指定图像?

发布于 2024-10-01 17:33:59 字数 644 浏览 6 评论 0原文

我有一个对象 MObject,当前定义如下:

+ (id)objectWithType:(NSString*)type name:(NSString*)name code:(NSString*)code imageName:(NSString*)imageName
{
 UIImage *theImage = [[UIImage alloc] imageNamed:imageName];
 MObject *newObject = [[[self alloc] init] autorelease];
 newObject.type = type;
 newObject.name = name;
 newObject.code = code;
 return newObject;
}

imageName 旨在包含相应图像 anImage.png 的文件名。一旦我将其放入,我需要创建 UIImage 对象,以便将其放置在 cell.imageView.image 中。

我 1) 不清楚我开始采取的方向(如上所示)是否可行,2) 到目前为止我不知道如何将此 UIImage 放入我的 MainViewController。我非常感谢您提供的任何帮助。

I have an object MObject that is currently defined like so:

+ (id)objectWithType:(NSString*)type name:(NSString*)name code:(NSString*)code imageName:(NSString*)imageName
{
 UIImage *theImage = [[UIImage alloc] imageNamed:imageName];
 MObject *newObject = [[[self alloc] init] autorelease];
 newObject.type = type;
 newObject.name = name;
 newObject.code = code;
 return newObject;
}

imageName is meant to contain the file name of the appropriate image anImage.png. Once I put that in, I need the UIImage object to be created so that it can be placed in cell.imageView.image.

I'm 1) unclear on whether the direction I've started to take (as shown above) is workable, 2) so far clueless as to how I can get this UIImage into my in my MainViewController. I really appreciate any help you offer.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你穿错了嫁妆 2024-10-08 17:33:59

我得到了它!我将原始帖子中显示的代码更改为:

+ (id)objectWithType:(NSString*)type name:(NSString*)name code:(NSString*)code imageName:(NSString*)imageName
{
 MObject *newObject = [[[self alloc] init] autorelease];
 newObject.type = type;
 newObject.name = name;
 newObject.code = code;
 newObject.imageName = imageName;
 return newObject;
}

然后我将以下行添加到 cellForRowAtIndexPath:

cell.imageView.image=[UIImage imagedNamed:object.imageName];

这就是全部!

I got it! I changed the code shown in my original post to:

+ (id)objectWithType:(NSString*)type name:(NSString*)name code:(NSString*)code imageName:(NSString*)imageName
{
 MObject *newObject = [[[self alloc] init] autorelease];
 newObject.type = type;
 newObject.name = name;
 newObject.code = code;
 newObject.imageName = imageName;
 return newObject;
}

Then I added the following line to cellForRowAtIndexPath:

cell.imageView.image=[UIImage imagedNamed:object.imageName];

And that's all it took!

北渚 2024-10-08 17:33:59
  • (UIImage *)newImageFromResource:(NSString *)文件名
    {
    NSString *str = [[[NSBundle mainBundle]资源路径] stringByAppendingPathComponent:文件名];
    UIImage *image = [[UIImage alloc] initWithContentsOfFile:str];
    返回图像;
    每当

你想设置图像

UIImage *img=[UIImage newImageFromResource:@"xyz.png"];

您可以在任何您想要的地方设置 img &别忘了释放img

  • (UIImage *)newImageFromResource:(NSString *)filename
    {
    NSString *str = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:filename];
    UIImage *image = [[UIImage alloc] initWithContentsOfFile:str];
    return image;
    }

and Whenever you want to set Image

UIImage *img=[UIImage newImageFromResource:@"xyz.png"];

You can set img wherever you want & dont forget to release img

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文