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。我不明白为什么……这里有任何新鲜的烈酒可以为我提供一些解决的线索吗?
谢谢。
I've got a code snippet here that I can't make working.
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];
}
The problem here is that : img1 and img2, are not images but rather NSArray with multiple entries. Ican't figure why... dos any fresh spirit here could provide me with some clue to fix.
maaany thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
让我们从第一个 for 循环中的内存问题开始。存在过度释放:
第二个内存问题。 CoupleTexture 已过度释放。
您应该学习如何进行快速枚举。它应该如下所示:
掩码 for 循环中存在更多内存问题:
因此掩码数组未释放...以及循环后立即创建的列表数组...
哦,天哪。在这里我必须停下来,因为我也开始头痛了;)你应该学习的东西:
释放
你通过init
创建的内容或你保留
的内容显式地不要沮丧。继续学习:)
Lets start with a memory issue in the first for loop. There's an overrelease:
Second memory issue.
coupleTexture
is overreleased.The you should learn how to do fast enumerations. It should look like this:
More memory issue in the masks for loop:
Therefore the masks array isn't released... as well as the list array created right after the loop...
Oh boy. Here I have to stop since I also start getting headaches ;) Things you should learn:
release
what you created viainit
or what youretained
explicitlyDon't get frustrated. Keep on learning :)
这段代码让我头疼。您确实需要清理它并封装在某些方法或函数中,特别是如果您坚持一遍又一遍地重新分配相同的变量名称。
我认为你的问题在这里:
首先分配一个由空数组初始化的数组,然后为它分配纹理中的数组,然后释放分配的最后一个数组。它认为这是对texturesBoard 数组进行加扰。它很可能会杀死其中一个数组,从而导致数组大小出现错误。
它应该只是:
This code gave me a headache. You really need to clean this up and encapsulate in some methods or functions, especially if you insist on reassigning the same variable names over and over again.
I think your problem is here:
First you assign an initialized by empty array, then you assign it the arrays in textures then you release the last array you assigned. It think that is scrambling the texturesBoard array. It most likely kills one of the arrays making the array size come out wrong.
It should just be: