2010-05-11 39 views
1

我有一个代码片断,我无法工作。NSArray中的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谢谢。

+0

你真的应该“接受”一个答案。在我看来,@ stigi的答案是正确的。 @stigi竭尽全力编写了一个写得很好,可重用的响应。请点击它左边的复选标记以接受。 – Jann 2010-05-11 22:28:35

回答

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 

因此口罩阵列没有被释放......以及列表后右侧创建阵列循环...

哦,男孩。在这里,我不得不停止,因为我也开始越来越头痛;)事情你应该学会:

  • 您通过init创建只有release什么,或者你retained明确
  • 用正确的方式
  • 结构快速列举你的代码...也许提取一些for循环来分离方法并单独调试它们的结果。

不要沮丧。继续学习:)

+0

非常感谢你的回答,真的很抱歉,我会用你的建议和干净的东西。 欢呼声。 – Tibi 2010-05-12 08:29:55

0

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

我觉得你的问题是在这里:

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

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

应该仅仅是:

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