ios - CATiledLayer subview 导致内存警告和崩溃

标签 ios map catiledlayer

关闭。这个问题需要debugging details .它目前不接受答案。












想改进这个问题?将问题更新为 on-topic对于堆栈溢出。

4年前关闭。




Improve this question




好吧,这就是正在发生的事情......我有一个 UIView 子类,它包括两个点(开始和结束),这两个点之间的 CGPath 和很多 UILabel。如果我将该 UIView 子类作为 subview 添加到我的 ScrollView (在基于 CATiledLayer 的 UIView 之上),那么我的应用程序开始收到内存警告并最终崩溃。但是,如果我删除包含点和路径的 UIView 子类并保持 CATiledLayer View 保持原样,那么一切都可以正常运行,并且不会发生崩溃或内存警告。

有没有人知道为什么会这样?内容 View 应该不会有任何问题,因为我只绘制最大 40px 宽的标签和一个也很小的 CGPath?

这是一个 map 应用程序

这是一些代码:

 //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];
 }

这是我的路径 View 类,它是上面 UMMapContentView View 中的另一个 subview :
+ (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 View 中的方法:
- (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];
    }
}

我不确定我需要朝哪个方向前进...任何帮助将不胜感激! :)

最佳答案

你没有给出UMMapContentView的源代码,但如果它使用 CATiledLayer,那么这可能是问题所在。您似乎无法子查看 CATiledLayer 支持的 View 。

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

如果您无论如何编写自己的平铺层,可能会更容易。 CATiledLayer 上的文档很少,而错误很多。

关于ios - CATiledLayer subview 导致内存警告和崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15193467/

相关文章:

ios - 来自 UITableviewcell 的弹出窗口不起作用

ios - UIView 转换矩形 : - finding subview position two views deep

android - 如何在android中的图像上的两点之间画线?

iphone - 在 UIScrollView 中为 zoomScale 设置动画时不需要的滚动

ios - 将 SDWebImage 与解析一起使用

ios - shouldChangeCharactersInRange 没有被调用。?

scala - 在遍历 map 时过滤掉键

qt - Qt的映射小部件

ios - 无法在 MonoTouch 中设置 CATiledLayer.FadeDuration

iphone - CATiledLayer 在绘制内容之前消隐图 block