CATiledLayer子视图导致内存警告和崩溃

时间:2013-03-04 02:05:29

标签: ios map catiledlayer

好的,这就是发生了什么...我有一个UIView子类,它包括两个点(开始和结束),这两个点之间的CGPath和许多UILabel。如果我将UIView子类作为子视图添加到我的scrollview(在我的基于CATiledLayer的UIView的顶部),那么我的应用程序开始获取内存警告并最终崩溃。但是,如果我删除包含点和路径的UIView子类并保留CATiledLayer视图,那么一切都能正常运行,并且不会发生崩溃或内存警告。

有没有人对为什么会这样做有任何想法?内容视图应该没有任何问题,因为我只绘制最大40px宽的标签和CGPath,这也很小?

这是一个地图应用程序

以下是一些代码:

 //Display the map image
 - (void)displayTiledImageNamed:(NSString *)imageName size:(CGSize)imageSize {
     self.zoomScale = 1.0;

     container = [[UIView alloc] initWithFrame:(CGRect){ CGPointZero, imageSize }];

     _zoomView = [[UIImageView alloc] initWithFrame:(CGRect){ CGPointZero, imageSize }];
     NSString *path = [NSString stringWithFormat:@"%@/%@-Small.png",[[NSBundle mainBundle] bundlePath], [imageName stringByDeletingPathExtension]];
     UIImage *zoomImage = [UIImage imageWithContentsOfFile:path];
     [_zoomView setImage:zoomImage];

     [container addSubview:_zoomView];
     [_zoomView release];

     _tilingView = [[UMTileView alloc] initWithImageName:imageName size:imageSize];
     _tilingView.frame = _zoomView.bounds;
     [_zoomView addSubview:_tilingView];
     [_tilingView release];

     [self configureForImageSize:imageSize];

     //This is the view that's causing the crash and memory warnings
     //If this view is commented out the app functions just fine
     UMMapContentView *m = [[UMMapContentView alloc] initWithFrame:CGRectMake(0,0,imageSize.width,imageSize.height) andColors:self.colors mapView:self.mapView];
    self.contentView = m;
     [container addSubview:self.contentView];
     [m release];

     [self addSubview:container];
     [container release];
 }

这是我的路径视图类,它是上面UMMapContentView视图中的另一个子视图:

+ (Class)layerClass {
    return [CATiledLayer class];
}

+ (CFTimeInterval)fadeDuration {
    return 0.0;
}

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.userInteractionEnabled = NO;
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}

- (void)dealloc {
    self.path = nil;
    self.pathColor = nil;
    [super dealloc];
}

- (void)resetPathView {
    CATiledLayer *tiledLayer = (CATiledLayer*)self.layer;
    tiledLayer.contents = nil;
    [tiledLayer setNeedsDisplay];
}

- (void)drawPath:(UMPath*)p {
    self.path = p;
    [self setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect {
    if (self.path) {
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGContextSetStrokeColorWithColor(context, self.pathColor.CGColor);
        CGContextSetShadowWithColor(context, CGSizeMake(2.0, 1.0), 1.0, [[UIColor colorWithWhite:0.2 alpha:0.8] CGColor]);
        CGContextSetLineWidth(context, 8.0);
        CGContextBeginPath (context);
        CGContextMoveToPoint (context, ((UMMapPoint*)[self.path.points objectAtIndex: 0]).x, ((UMMapPoint*)[self.path.points objectAtIndex: 0]).y);
        for (int i = 1; i < self.path.points.count; i++) {
            CGContextAddQuadCurveToPoint (context, ((UMMapPoint*)[self.path.points objectAtIndex:i-1]).x, ((UMMapPoint*)[self.path.points objectAtIndex: i-1]).y, ((UMMapPoint*)[self.path.points objectAtIndex:i]).x, ((UMMapPoint*)[self.path.points objectAtIndex:i]).y);
        }
        CGContextStrokePath (context);
    }
}

这是我将所有UILabel放在UMMapContentView视图中的方法:

- (void)mapAllLabelsInNavigationMap:(int)navigationMap {

    //There are 1109 label dictionaries in the labels array
    for (NSDictionary *dict in labels) {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake([[dict objectForKey:@"X"] floatValue],[[dict objectForKey:@"Y"] floatValue],[[dict objectForKey:@"Width"] floatValue],[[dict objectForKey:@"Height"] floatValue])];
        [label setLineBreakMode:NSLineBreakByWordWrapping];
        [label setNumberOfLines:0];
        [label setBackgroundColor:[UIColor clearColor]];
        [label setTextColor:[self colorFromHexString:[dict objectForKey:@"Foreground"]]];
        [label setFont:[UIFont systemFontOfSize:[[dict objectForKey:@"FontSize"] floatValue]]];
        [label setTextAlignment:NSTextAlignmentCenter];
        [label setText:[[UMDataBase shared] stringForID:[[dict objectForKey:@"Texts_ID"] intValue] withLanguage:languageID]];

        [self addSubview:label];
        [localMapLabels addObject:label];
        [label release];
    }
}

我不确定我需要去哪个方向......任何帮助都会非常感激! :)

1 个答案:

答案 0 :(得分:0)

您没有提供UMMapContentView的源代码,但如果它使用CATiledLayer,则可能是问题所在。您似乎无法查看CATiledLayer支持的视图。

滚动到底部(在评论部分):http://red-glasses.com/index.php/tutorials/catiledlayer-how-to-use-it-how-it-works-what-it-does/

如果你自己编写自己的平铺图层可能会更容易。关于CATiledLayer的文档很少,而bug很多。