如何添加UIImages到NSMutableArray,并更改图像? [英] How to add UIImages to an NSMutableArray, and change the images later?

查看:94
本文介绍了如何添加UIImages到NSMutableArray,并更改图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个简单的游戏,要求我在许多不同的地方绘制多个UIImages。我的问题是,我需要添加UIImages到NSMutableArray,并稍后访问它们。使用NSStrings来表示图像(例如路径)不会在这里工作,所以我需要知道如何更改数组中每个UIImage的图像。

I am making a simple game that requires that I draw multiple UIImages in many different places. My problem here is that I need to add the UIImages to an NSMutableArray, and access them later. Using NSStrings to represent the images (such as for paths) will not work here, so I need to know how to change the images for every UIImage in the array.

我的代码如下(至少,用于访问NSMutableArray)。 NSMutableArray在我的.h文件中声明,并以不同的方法初始化。

My code is as follows (at least, for accessing the NSMutableArray). The NSMutableArray is declared in my .h file, and is initialized in a different method.

for (int a = 0; a <= [theArray count]; a ++) {
    // I make a UIImage, which I will draw later
    UIImage *theImage = [theArray objectAtIndex:a];
    // then I do the drawing
}

我的问题是,我不知道如何更改NSMutableArray中的特定对象。我可以这样做?

This works fine. My problem is that I cannot figure out how to change a particular object in the NSMutableArray. How can I do this?

顺便说一句,我用下面的代码添加UIImages到NSMutableArray。

By the way, I am adding the UIImages to the NSMutableArray with the following code.

+ (void) createCarWithImage:(NSString*)theImageName {
    UIImage *anImage= [UIImage imageNamed:theImageName];
    [theArray addObject:anImage];
}


推荐答案

这个方法:

- (void)replaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject

例如:

[theArray replaceObjectAtIndex:4 withObject:someOtherImage];

这篇关于如何添加UIImages到NSMutableArray,并更改图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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