Memory not being released for MKMapView w/ ARC

前端 未结 3 1974
悲哀的现实
悲哀的现实 2021-01-02 07:58

I have a custom UIView called ActivityDetailView that I instantiate and then add to a scrollview within a parent view controller. When this custom

相关标签:
3条回答
  • 2021-01-02 08:32

    This is not really the best way to create a custom view from your nib file. What I suggest you do is to add the view that you take from the .nib file as a subview to self. The code should look something like this.

    -(id)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self)
        {
            NSArray *xibViews = [[NSBundle mainBundle] loadNibNamed:@"ActivityDetailView" owner:nil options:nil];
            if ([xibViews count] < 1) return nil;
            ActivityDetailView * xibView = [xibViews objectAtIndex:0];
            [xibView setFrame:frame];
            [self addSubview:xibView]; // This is where I have changed your code
        }
        return self;
    }
    
    0 讨论(0)
  • 2021-01-02 08:34

    You are probably facing a problem with cache.

    If you are using a lot of images maybe a good option is to remove this images from the xib and add it using code. Or if you load the images using [UIImage imageNamed:@""]

    But use something like that:

    NSData* dataImg = [NSData dataWithContentsOfFile:@"yourfile.png"];
    UIImage* img = [UIImage imageWithData:dataImg];
    

    To better help I need to know how you start your tables and also the others features.

    0 讨论(0)
  • 2021-01-02 08:37

    As it turns out, there is a known bug with iOS 6 and MKMapView not releasing it's memory correctly. Unfortunately there is no real fix other than trying to force the map view to purge it's cache which doesn't have that great of an impact on releasing memory.

    The strange thing is that even when the app is receiving memory warnings, the map view cache is still not being purged properly. Eventually, the app becomes unstable and is killed by the OS.

    Apple has been getting very sloppy with their testing lately. This is the second bug with MKMapView that I've come across (the other being mapViewDidFinishLoadingMap: being called early) and both bugs have been really obvious to catch if they had just done some performance and sanity testing.

    Here is some code which may help:

    - (void)viewDidDisappear:(BOOL)animated
    {
        [super viewDidDisappear:animated];
    
        // This is for a bug in MKMapView for iOS6
        [self purgeMapMemory];
    }
    
    // This is for a bug in MKMapView for iOS6
    // Try to purge some of the memory being allocated by the map
    - (void)purgeMapMemory
    {
        // Switching map types causes cache purging, so switch to a different map type
        detailView.mapView.mapType = MKMapTypeStandard;
        [detailView.mapView removeFromSuperview];
        detailView.mapView = nil;
    }
    

    The other thing you could do is use one instance of the MKMapView throughout your entire app and that should help minimize how much memory is allocated each time the view is shown since the map view will already be allocated.

    0 讨论(0)
提交回复
热议问题