我正在尝试在ImageView控件上添加一个子视图,我认为,我正确地分配了视图和dealloc视图。这是我的代码。
for(int Count =0; Count < [list count]; Count++)
{
MapCallout *callout = [list objectAtIndex:Count];
CGSize constSize = { 700.0f, 40.0f };
NSString * sTmp = [callout.Name stringByAppendingString:@"WW"];
CGSize textSize = [sTmp sizeWithFont:[UIFont fontWithName:@"Arial-BoldMT" size:25.0] constrainedToSize:constSize lineBreakMode:UILineBreakModeClip];
int xValue = callout.X;
int yValue = callout.Y;
int position = 0;
UIImage *calloutImage;
if(callout.Direction==0)
{
calloutImage=[UIImage newImageFromResource:@"arrow-0.png"];
callout.Y=callout.Y-3; position= -3;
}
else if(callout.Direction==1)
{
calloutImage=[UIImage newImageFromResource:@"arrow-1.png"];
callout.Y=callout.Y-3; position= -3;
}
else if(callout.Direction==2)
{
callout.Y=callout.Y + 2;
calloutImage=[UIImage newImageFromResource:@"arrow_2.png"];
position= +2;
}
CalloutButton *calloutButton = nil;
int arraySize = [dequeueReusableCallout count] - 1;
if(Count > arraySize)
{
calloutButton = [[[CalloutButton alloc]initWithFrame:CGRectMake(xValue,
self.tapDetectingImageView.frame.origin.y + yValue ,
textSize.width,40)] autorelease];
[calloutButton setDelegate:self];
[dequeueReusableCallout addObject:calloutButton];
}
calloutButton = [dequeueReusableCallout objectAtIndex:Count];
[calloutButton setFrame:CGRectMake(xValue,
self.tapDetectingImageView.frame.origin.y + yValue ,
textSize.width,40)] ;
[calloutButton setBackgroundImage:calloutImage andText:callout.Name andYvalue:position];
calloutButton.tag = callout.ID;
calloutButton.ViewMapType = [callout.calloutType intValue];
calloutButton.isLive = callout.isLive;
calloutButton.isAnimated = callout.isAnimated;
[self.tapDetectingImageView addSubview:calloutButton];
}
//Callout Button Method
-(void) setBackgroundImage:(UIImage*)image andText:(NSString*)text andYvalue:(double)Y
{
if(!btnCallout)
btnCallout = [[UIButton alloc] init];
[btnCallout setFrame:CGRectMake(0, -Y, self.frame.size.width, self.frame.size.height)];
[btnCallout setBackgroundImage:image forState:UIControlStateNormal];
[btnCallout addTarget:self action:@selector(CalloutTouch:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:btnCallout];
if(!lblCallout)
lblCallout = [[UILabel alloc] init];
[lblCallout setFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
lblCallout.backgroundColor = [UIColor clearColor];
lblCallout.font = [UIFont systemFontOfSize:42.0];
lblCallout.userInteractionEnabled = NO;
lblCallout.text = text;
lblCallout.textAlignment = UITextAlignmentCenter;
lblCallout.font = [UIFont fontWithName:@"Arial-BoldMT" size:25.0];
[self addSubview:lblCallout];
}
[self viewForZoomingInScrollView:self.mapScrollView];
}
如果我多次调用此方法方法,则会发生内存警告并且应用程序崩溃。 在这里, [self.tapDetectingImageView addSubview:calloutButton]; 我如果对此语句进行评论,则表明它正常工作。请让我知道我错在哪里。
答案 0 :(得分:1)
在将[calloutButton release]
数组添加到dequeueReusableCallout
之后,应该调用{{1}},因为数组现在在新分配的实例上有一个保留