2

Alright, here's what's happening... I have a UIView subclass, which includes two points (start and end), a CGPath between those two points and lot of UILabels. If I add that UIView subclass as a subview to my scrollview (over top of my CATiledLayer based UIView) then my app starts getting memory warnings and eventually crashes. However, if I remove the UIView subclass that contains the points and path and leave the CATiledLayer view as it is, everything functions perfectly and no crashing or memory warnings occur.

Does anyone have any ideas on why this is occurring? The content view shouldn't have any problems being so big should it since I'm only draw labels that are at max 40px wide and a CGPath, which is also pretty small?

This is a mapping application

Here's some code:

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

This my path view class which is another subview inside the UMMapContentView view above:

+ (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);
    }
}

And this is my method that places all the UILabels in the 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];
    }
}

I'm not really sure what direction I need to head... Any help would be much appreciated! :)

Homeschooldev
  • 405
  • 1
  • 7
  • 18

1 Answers1

0

You did not give the source code of UMMapContentView, but if it uses CATiledLayer, then that may be the problem. It seems that you cannot subview a CATiledLayer-backed view.

Scroll to the bottom (in the comments section): http://red-glasses.com/index.php/tutorials/catiledlayer-how-to-use-it-how-it-works-what-it-does/

It may be easier if you write your own tiled layer anyhow. Documentation on CATiledLayer is sparse while the bugs are aplenty.

jlukanta
  • 159
  • 1
  • 11