在视图控制器UI和地图中的每个位置随机崩溃



我在后台更新 UI 时崩溃

'NSGenericException', reason: '*** Collection <CALayerArray: 0x172c50bf0> was mutated while being enumerated.'

这是我的代码也随机崩溃并占用大量内存我做错了什么,请建议我。对不起我的英语

[weakSelf performSelectorInBackground:@selector(googleMapView) withObject:nil];
googleMapView{
[mapView animateToZoom:15];
int k=0;
@try {
GMSMutablePath *path = [GMSMutablePath path];
for(DRMapView * object in self.mapViewDataArray)
{
mMapViewObj=object;
GMSMarker * markersLocal = [[GMSMarker alloc] init];
pinImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,40+20*isiphone6Plus()+20*isiphone6(),60)];
markersLocal.position = CLLocationCoordinate2DMake(mMapViewObj.mLatitude, mMapViewObj.mLongitude);
[path addCoordinate:  markersLocal.position];

view = [[UIView alloc] initWithFrame:CGRectMake(0,0,40+20*isiphone6Plus()+20*isiphone6(),60)];
view.backgroundColor=[UIColor blackColor];
pinImageView.contentMode = UIViewContentModeScaleAspectFit;
NSString * imageWithPercent;
label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 12.5, 12.5)];
if ([mMapViewObj.mGenderRatio isEqualToString:@""])
{
label.text =@"0";
imageWithPercent =[NSString stringWithFormat:MAP_NO_ONE];
pinImageView.image = [UIImage imageNamed:imageWithPercent];
}
else
{
//              NSString *genderRatio =[NSString stringWithFormat:@"%@",[DrinkrCommon valueOrNil:[[dict objectForKey:kGenderRatio] objectForKey:kMale]]];
label.text =mMapViewObj.mTotalMembers;
int percentage = [mMapViewObj.mMaleFemaleRatio intValue];
if (percentage >= 80) {
imageWithPercent =[NSString stringWithFormat:@"map80"];
//NSLog(@"80====>>> %d",percentage);
}
else if (percentage >= 60) {
// NSLog(@"60====>>> %d",percentage);
imageWithPercent =[NSString stringWithFormat:@"map60"];
}
else if (percentage == 50) {
// NSLog(@"50====>>> %d",percentage);
imageWithPercent =[NSString stringWithFormat:@"map50"];
}
else if (percentage >= 40) {
// NSLog(@"40====>>> %d",percentage);
imageWithPercent =[NSString stringWithFormat:@"map40"];
}
else if (percentage >= 20) {
//  NSLog(@"20====>>> %d",percentage);
imageWithPercent =[NSString stringWithFormat:@"map20"];
}
else if (percentage <= 20) {
//  NSLog(@"<20====>>> %d",percentage);
imageWithPercent =[NSString stringWithFormat:@"map100"];
}
pinImageView.image = [UIImage imageNamed:imageWithPercent];
}
pinImageView.contentMode = UIViewContentModeScaleAspectFit;
[label setTextColor:[UIColor whiteColor]];
[label setBackgroundColor:[UIColor blackColor]];
[label sizeToFit];
label.layer.cornerRadius=label.frame.size.height / 2;
label.layer.masksToBounds = YES;
label.adjustsFontSizeToFitWidth=YES;
label.center=pinImageView.center;
view.center=pinImageView.center;
view.backgroundColor=[UIColor clearColor];
[view addSubview:pinImageView];
[view addSubview:label];
UIImage * markerIcon = [self imageFromView:view];
markersLocal.userData=[NSNumber numberWithInt:k+1];
markerView = [[UIImageView alloc] initWithImage:markerIcon];
markerView.tintColor = [UIColor blackColor];
markersLocal.iconView = markerView;
//    markers.map = mapView;
marker=markersLocal;
//        marker=m/MapViewObj.viewMarker;
marker.map=mapView;
k++;
}
GMSCoordinateBounds * bounds = [[GMSCoordinateBounds alloc] initWithPath:path];
[mapView animateWithCameraUpdate:[GMSCameraUpdate fitBounds:bounds withPadding:20.0f]];
}
@catch (NSException *exception) {
}
@finally {
}
}

如您所见,异常是关于一个集合(在本例中它是一个数组(正在被修改,同时它也被枚举。

"集合CALayerArray"是指层数组(UIView 的实例,它们都由层备份(和修改发生在您调用removeFromSuperviewaddSubview时。枚举可以在重绘期间随时发生。

所以我认为你的问题可能出在这 2 行:

[view addSubview:pinImageView];
[view addSubview:label];

最新更新