我必須添加一個UIImageView作爲MapView的子視圖。爲此,我在MapView上創建了一個圖層。在這一層我想把我的形象,但我得到一個白色的矩形,沒有別的。我的圖像不可見。在CALayer中添加UIImage
這是代碼:
- (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];
}
我想setNeedsDisplay使圖層重繪內容,因此刪除圖像。 – Bastian
這也有效。謝謝阿德里安娜。 –