【问题标题】:iOS : Loading GMSMapView consuming loads of memory and decrease in FPS(Frames Per Second) rateiOS:加载 GMSMapView 会消耗大量内存并降低 FPS(每秒帧数)速率
【发布时间】:2015-02-20 05:36:34
【问题描述】:

我创建了一个谷歌map 视图,我在其中放置了一些注释并在加载时放置它们,但是当我不断更改我的相机视图时,内存消耗变得太高了大约 200MB。 FPS 速率也从 30 降低到 5 或 6。我的应用程序只会崩溃。我怎样才能释放那段记忆?这是我在移动相机角度时所做的代码,

- (void)mapView:(GMSMapView *)mapview didChangeCameraPosition:(GMSCameraPosition *)position 
{
    [self updateCenterOfScreenMarker:position.target];
}

- (void)updateCenterOfScreenMarker:(CLLocationCoordinate2D)coordinate {
       [self updateMarkers:currentLocation];
    CLLocation * centerOfGreen = [self getLocationCenterOfGreen];
    if (centerOfGreen) {
        CLLocation * currentCamera = [[CLLocation alloc] initWithLatitude:coordinate.latitude longitude:coordinate.longitude];
        CLLocationDistance distance = [currentCamera distanceFromLocation:centerOfGreen];
        distanceFromCameraToGolfer = [currentCamera distanceFromLocation:currentLocation];
        [currentCamera release];

        distanceFromCameraToCenterOfGreen = distance;
        if (isYards) {
            distanceFromCameraToCenterOfGreen *= METERS_TO_YARDS;
            distanceFromCameraToGolfer *= METERS_TO_YARDS;
        }

        if (centerMarker) {
            centerMarker.map = nil; // You can remove a marker from the map by setting your GMSMarker's map property to nil.
            [centerMarker release];
            centerMarker = nil;
            headingLabel.text=@"";
            headingLabel=nil;
            headingLabel.text=nil;
            headingLabel2.text=@"";
            headingLabel2=nil;
            headingLabel2.text=nil;
            headingLabel3.text=@"";
            headingLabel3=nil;
            headingLabel3.text=nil;
            headingLabel4.text=@"";
            headingLabel4=nil;
            headingLabel4.text=nil;
            centerMarker.icon =nil;
        }


        if (mapView) {
//            centerMarker = [GMSMarker markerWithPosition:coordinate];
            headingLabel.text=@"";
            headingLabel2.text=@"";
            headingLabel3.text=@"";
            headingLabel4.text=@"";

            if(distanceFromCameraToGolfer >= 1000)
            {
                headingLabel = [[UILabel alloc] initWithFrame:CGRectMake(40, 180, 120, 30)];
                headingLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(160, 180, 40, 30)];
                headingLabel3 = [[UILabel alloc] initWithFrame:CGRectMake(200, 180, 20, 30)];
                headingLabel4 = [[UILabel alloc] initWithFrame:CGRectMake(225, 180, 70, 30)];
            }
            else
            {
                headingLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 180, 100, 30)];
                headingLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(125, 180, 40, 30)];
                headingLabel3 = [[UILabel alloc] initWithFrame:CGRectMake(170, 180, 20, 30)];
                headingLabel4 = [[UILabel alloc] initWithFrame:CGRectMake(195, 180, 70, 30)];
            }
            headingLabel.font=[UIFont fontWithName:@"Roboto-Bold" size:30];
            headingLabel2.font=[UIFont fontWithName:@"Roboto-Regular" size:30];
            headingLabel3.font=[UIFont fontWithName:@"Roboto-Light" size:30];
            headingLabel4.font=[UIFont fontWithName:@"Roboto-Bold" size:30];

            headingLabel.textColor=[UIColor whiteColor];
            headingLabel2.textColor=[UIColor whiteColor];
            headingLabel3.textColor=[UIColor yellowColor];
            headingLabel4.textColor=[UIColor whiteColor];

            headingLabel.textAlignment=NSTextAlignmentRight;
            headingLabel2.textAlignment=NSTextAlignmentCenter;
            headingLabel3.textAlignment=NSTextAlignmentCenter;
            headingLabel4.textAlignment=NSTextAlignmentLeft;


            headingLabel.text=nil;
            headingLabel2.text=nil;
            headingLabel3.text=nil;
            headingLabel4.text=nil;

//            centerMarker.title = nil;
//            centerMarker.snippet = nil;
//            centerMarker.groundAnchor = CGPointMake(0.5, 0.5);
            str=@"";
            str = [NSString stringWithFormat:@"%d",distanceFromCameraToGolfer];
            str2=@"";

            NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
            NSString * units = [defaults objectForKey:PREFERENCE_KEY_UNITS];
            if ([units isEqualToString:@"yards"]) {
                str2 = [NSString stringWithFormat:@"Yd"]; 
            }
            else
            {
                str2 = [NSString stringWithFormat:@" m"];
            }

            str3=@"";
            str3 = [NSString stringWithFormat:@"/"];
            str4=@"";
            str4 = [NSString stringWithFormat:@"%d", distanceFromCameraToCenterOfGreen];

            if([str4 intValue]>=0 && [str4 intValue]<=14)
            {
                headingLabel.hidden=YES;
                headingLabel2.hidden=YES;
                headingLabel3.hidden=YES;
                headingLabel4.hidden=YES;
            }

            headingLabel.text=str;
            headingLabel2.text=str2;
            headingLabel3.text=str3;
            headingLabel4.text=str4;
            [mapView addSubview:headingLabel];
            [mapView addSubview:headingLabel2];
            [mapView addSubview:headingLabel3];
            [mapView addSubview:headingLabel4];
//            centerMarker.icon = [self addText:[UIImage imageNamed:@"grid.png"] text:@""  red:255 green:255 blue:255];
//            centerMarker.map = mapView;
//            centerMarker.tappable=NO;
            [centerMarker retain];
            [headingLabel release];
            [headingLabel2 release];
            [headingLabel3 release];
            [headingLabel4 release];

        } // mapView

    }

}

在上面的代码中,标题标签是显示从当前位置到相机位置的距离的标签。 以下是内存消耗和 FPS 的图像。

【问题讨论】:

标签: ios google-maps memory-management frame-rate gmsmapview


【解决方案1】:

你可以试试 更改此委托方法:

- (void)mapView:(GMSMapView *)mapView idleAtCameraPosition:(GMSCameraPosition *)position{
    [self updateCenterOfScreenMarker:position.target];
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-14
    • 2014-06-26
    • 1970-01-01
    • 1970-01-01
    • 2019-02-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多