我打電話給Web服務,該服務返回一個代理方法中的註釋數組,該方法將使用addAnnotations
方法添加到我的地圖中,作爲MKMapView
。一切順利順順當當,直到委託方法發送快速連續兩個數組(通常約150毫秒 - 500毫秒),然後我得到的這條線[kMap addAnnotations:tileArray];
一個EXC_BAD_ACCESS (code=1 address 0x20)
- 這似乎是一個記憶的問題,但我真的不知道該怎麼做如何或如何改變我的代碼來解決它。EXC_BAD_ACCESS在MKMapView中添加註釋
這裏是委託方法
-(void)rebelBaseManager:(RebelBaseManager *)manager didUpdateTileHour:(NSArray *)tileArray boundaryBreak:(NSString *)breakType atTileLevel:(int)callTileLevel {
if (timerStarted == NO) {
[self startTimer];
}
//Check for tileLevel in case multiple calls were made at different tile levels
if (tileLevel == callTileLevel) {
[kMap addAnnotations:tileArray];
[HourInMap addObjectsFromArray:tileArray];
}
}
我還添加了一個方法,讓我的動畫去除註釋這是在以下情況下,它的確與衆不同:
- (void)removeAnnotationsWithFade:(NSArray *)annotations animated:(BOOL)shouldAnimate {
if (!shouldAnimate)
[self removeAnnotations:annotations];
else {
for (HourAnnotation *annotation in annotations) {
MKAnnotationView *annotationView = [self viewForAnnotation:annotation];
[UIView animateWithDuration:2
animations:^{
annotationView.alpha =0;
}completion:^(BOOL finished) {
[self removeAnnotation:annotation];
}];
}
}
--- ------ ADDITION ---------
在我的代碼中添加一個自定義的回覆下面Rob的回答#3中的符號。
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
- (id)initWithHourAnnotation:(HourAnnotation *)hourAnnotation reuseIdentifier:(NSString *)reuseIdentifier {
CGRect myFrame = self.frame;
myFrame.size.width = hourAnnotation.frameSize;
myFrame.size.height = hourAnnotation.frameSize;
self = [super initWithFrame:myFrame];
//When I use this here I seem to get the frame and color of the old annotation displayed
//self = [super initWithAnnotation:velocityAnnotation reuseIdentifier:reuseIdentifier];
if (self)
{
self.layer.cornerRadius = self.frame.size.width/2;
self.clipsToBounds = YES;
[self setBackgroundColor:[UIColor clearColor]];
NSArray *alphaValue = [[NSArray alloc]initWithArray:[self alphaForTileLevel]];
self.fillColor = [UIColor colorWithHue:hourAnnotation.color saturation:.06 brightness:.23 alpha:[[alphaValue objectAtIndex:hourAnnotation.tileLevel-1]doubleValue]];
self.strokeColor = [UIColor colorWithHue:hourAnnotation.color saturation:.06 brightness:.23 alpha:.35];
self.enabled = NO;
self.canShowCallout = NO;
self.userInteractionEnabled = NO;
}
return self;
}
- (void)drawRect:(CGRect)rect
{
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:rect];
[self.fillColor set];
[path fill];
[self.strokeColor set];
[path setLineWidth:2.0f];
[path stroke];
}
可能下面的副本,雖然不完全清楚:http://stackoverflow.com/questions/4202510/mkmapview-crashing-with-exc-bad-access – Rob 2014-08-28 12:56:13
@Rob這個答案的問題,你參考(http://stackoverflow.com/a/8932791/2939977)沒有幫助,因爲我得到了一些無效的座標,但我也看到你在下面你的答案解決的幾個內存泄漏。我正在研究那些現在的 – 2014-08-28 20:43:31
'kMap'定義在哪裏? – jlehr 2014-08-28 21:23:15