NSArray中的NSArray不会返回我想要的图像

时间:2010-05-11 20:02:07

标签: iphone-sdk-3.0 nsmutablearray nsarray

我在这里有一段代码片段,我无法正常工作。

NSUInteger i;
//NSMutableArray *textures = [[NSMutableArray alloc] initWithCapacity:kNumTextures];
//NSMutableArray *texturesHighlighted = [[NSMutableArray alloc] initWithCapacity:kNumTextures];

NSMutableArray *textures= [[NSMutableArray alloc] init];


for (i = 1; i <= kNumTextures; i++)
{
    NSString *imageName = [NSString stringWithFormat:@"texture%d.png", i];
    NSString *imageNameHighlighted = [NSString stringWithFormat:@"texture%d_select.png", i];
    UIImage *image = [UIImage imageNamed:imageName];
    UIImage *imageHighlighted = [UIImage imageNamed:imageNameHighlighted];
    //NSArray *pics = [[NSArray alloc] initWithObjects:(UIImage)image,(UIImage)imageHighlighted,nil];
    NSArray *pics = [NSArray arrayWithObjects:image,imageHighlighted,nil];
    [textures addObject:pics];
    [pics release];
}

//select randomly the position of the picture that will be represented twice on the board
NSInteger randomTexture = arc4random()%([textures count]+1);

//extract image corresponding to the randomly selected index
//remove corresponding pictures from textures array
NSArray *coupleTexture = [textures objectAtIndex:randomTexture];
[textures removeObjectAtIndex:randomTexture];

//create the image array containing 1 couple + all other pictures
NSMutableArray *texturesBoard = [[NSMutableArray alloc] initWithCapacity:kNumPotatoes];

[texturesBoard addObject:coupleTexture];
[texturesBoard addObject:coupleTexture];
[coupleTexture release];

NSArray *pics = [[NSArray alloc] init];
for (pics in textures)  {
    [texturesBoard addObject:pics];
}
[pics release];

//shuffle the textures
//[texturesBoard shuffledMutableArray];

//Array with masks
NSMutableArray *masks= [[NSMutableArray alloc] init];

for (i = 1; i <= kNumMasks; i++)
{
    NSString *maskName = [NSString stringWithFormat:@"mask%d.png", i];
    UIImage *mask = [UIImage imageNamed:maskName];
    //NSArray *pics = [[NSArray alloc] initWithObjects:mask,nil];
    [masks addObject:mask];

    //[pics release];
    [maskName release];
    [mask release];
}

//Now mask all images in texturesBoard
NSMutableArray *list = [[NSMutableArray alloc] init];
for (i = 0; i <= kNumMasks-1; i++)
{
    //take on image couple from textures
    NSArray *imgArray = [texturesBoard objectAtIndex:i];
    UIImage *mask = [masks objectAtIndex:i];

    //mask it with the mask un the array at corresponding index
    UIImage *img1 =(UIImage *) [imgArray objectAtIndex:0];
    UIImage *img2 =(UIImage *) [imgArray objectAtIndex:1];
    UIImage *picsMasked = [self maskImage:(UIImage *)img1 withMask:(UIImage *)mask];
    UIImage *picsHighlightedMasked = [self maskImage:(UIImage *)img2 withMask:(UIImage *)mask];
    //Init image with highlighted status
    TapDetectingImageView *imageView = [[TapDetectingImageView alloc] initWithImage:picsMasked imageHighlighted:picsHighlightedMasked];
    [list addObject:imageView];
}

这里的问题是:img1和img2不是图像,而是具有多个条目的NSArray。我无法理解为什么......这里有任何新鲜精神可以为我提供一些线索来解决。

maaany谢谢。

2 个答案:

答案 0 :(得分:3)

让我们从第一个for循环中的内存问题开始。有一个过度发布:

NSArray *pics = [NSArray arrayWithObjects:image,imageHighlighted,nil];
[textures addObject:pics];
[pics release];   // <-- pics is overreleased since it is created autoreleased

第二个内存问题。 coupleTexture被过度释放。

NSArray *coupleTexture = [textures objectAtIndex:randomTexture];
[textures removeObjectAtIndex:randomTexture];

//create the image array containing 1 couple + all other pictures
NSMutableArray *texturesBoard = [[NSMutableArray alloc] initWithCapacity:kNumPotatoes];

[texturesBoard addObject:coupleTexture];
[texturesBoard addObject:coupleTexture];
[coupleTexture release];      // <--- coupleTexture is autoreleased since it is returned by objectAtIndex: and not retained

你应该学习如何快速枚举。它应该是这样的:

for (NSArray *pics in textures)  {
    [texturesBoard addObject:pics];
}

循环掩码中存在更多内存问题:

NSString *maskName = [NSString stringWithFormat:@"mask%d.png", i];
UIImage *mask = [UIImage imageNamed:maskName];
//NSArray *pics = [[NSArray alloc] initWithObjects:mask,nil];
[masks addObject:mask];

//[pics release];
[maskName release];     // <-- overreleased.. was created by stringWithFormat
[mask release];         // <-- overreleased.. same same

因此,不会释放mask数组......以及在循环之后创建的列表数组...

哦,小伙子。在这里,我必须停下来,因为我也开始头疼;)你应该学习的东西:

  • release您通过init创建的内容或retained明确
  • 的内容
  • 以正确的方式使用快速枚举
  • 构建代码......也许提取一些for循环来分离方法并分别调试它们的结果。

不要感到沮丧。继续学习:)

答案 1 :(得分:0)

这段代码让我很头疼。你真的需要清理它并封装在一些方法或函数中,特别是如果你坚持一遍又一遍地重新分配相同的变量名。

我认为你的问题在这里:

NSArray *pics = [[NSArray alloc] init];
for (pics in textures)  {
    [texturesBoard addObject:pics];
}
[pics release];

首先分配一个由空数组初始化,然后在纹理中为它分配数组,然后释放你指定的最后一个数组。它认为这是在加扰texturesBoard数组。它很可能杀死其中一个阵列,导致阵列大小错误。

应该是:

NSArray *pics;
for (pics in textures)  {
    [texturesBoard addObject:pics];
}