Adriana Carelli Adriana Carelli - 3 months ago 13
iOS Question

add UIImage in CALayer

I must add a UIImageView as subview of MapView. To do this I created a layer above the MapView. In this layer I want to put my image, but I get a white rectangle and nothing else. My image is not visible.

This is the code:

-(void) viewDidLoad{
//......

CALayer *layer = [CALayer layer];
layer.backgroundColor = [[UIColor whiteColor] CGColor];

if(UI_USER_INTERFACE_IDIOM()== UIUserInterfaceIdiomPhone){
layer.bounds = CGRectMake(self.mapView.bounds.origin.x,
self.mapView.bounds.origin.y, 80, 300);
}else{
layer.bounds = CGRectMake(self.mapView.frame.origin.x,
self.mapView.frame.origin.y, 150, 700);
}


layer.contents = (id)[UIImage imageNamed:@"myImage.png"];
//the name is correct but in the output the image is not visible

[[self.mapView layer] addSublayer:layer];
[layer setNeedsDisplay];

}

Answer

I removed

 [layer setNeedsDisplay];

I do not know why, but it works!

Comments