【发布时间】:2011-04-23 15:27:16
【问题描述】:
我有一个 TapDetectingImageView 类,它在使用后属于内存,因为我没有在我的代码中重用它。 我现在更改我的代码以重用它,而不是每次都重新创建一个新代码。
目标是配置 TapDetectingImageView 对象并将其放入视图中。我会做 8 次,但使用下面的代码,只显示最后一个。我确定我错过了什么,但是什么? ;)
持有imageViews的视图是potatoeView。我正在通过以下方式添加新的 TapDetectingImageView:
[potatoeView addSubview:imageView];
那段代码有什么问题?
for (int i = 0; i <= numberOfPotatoes-1; i++)
{
NSLog(@"potatoesView Generation : %d",i);
NSArray *imgArray = [[NSArray alloc] initWithArray:[p objectAtIndex:i]];
UIImage *img1 = (UIImage *) [imgArray objectAtIndex:0];
UIImage *img2 = (UIImage *) [imgArray objectAtIndex:1];
NSString *imgName = (NSString *) [imgArray objectAtIndex:2];
if (imageView == nil) {
imageView = [[TapDetectingImageView alloc] initWithImage:img1 imageHighlighted:img2];
}else {
[imageView setImage:(UIImage *)img1];
[imageView setImageHighlighted:(UIImage *)img2];
}
[imageView setDelegate:self];
// setup each frame to a default height and width, it will be properly placed when we call "updateScrollList"
CGRect rect = imageView.frame;
rect.size.height = [potatoesSize floatValue] ;
rect.size.width = [potatoesSize floatValue] ;
imageView.frame = rect;
[imageView setName:(NSString *)imgName];
imageView.tag = i+1; // tag our images for later use when we place them in serial fashion
NSValue *val = [potatoesSizeAndPositions objectAtIndex:i];
CGPoint point = [val CGPointValue];
imageView.center = point;
[potatoeView addSubview:imageView];
[imgArray release];
}
【问题讨论】:
标签: iphone cocoa-touch memory-management uiimageview