清除 MapView 的 UIImageView 子视图上的图像并绘制新图像

Clear Image on UIImageView subview of a MapView and draw new Image

我将子视图中的 Gif 图像绘制到 Mapview 上。 Gif 不仅仅是矩形。新图像将旋转,因此无法在旧图像上绘制新图像。在绘制新图像之前,我需要清除旧图像。我找不到任何解决方案来做到这一点。到目前为止,这是我的工作代码:

CALayer *layer = [CALayer layer];
layer.bounds = CGRectMake(0, 0, 260, 260);
CGPoint center = CGPointMake(150, 300);
layer.position = center;
layer.contents = (id) [UIImage imageNamed:@"Image.gif"].CGImage;
layer.affineTransform = CGAffineTransformMakeRotation(angleDegree * 3.14/180);
[[mapView layer] addSublayer:layer];

从 mapView 中删除图层。确保您持有对加载图像的图层的引用。

[layer removeFromSuperlayer];

编辑:

因为你没有给我提供太多代码,我会根据我对你情况的理解尽力提供解决方案,伙计:)

- (IBAction)selectTime:(id)sender {

    NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
    [outputFormatter setDateFormat:@"HH"];
    NSString *eventHours = [outputFormatter stringFromDate:timePicker.date];
    currentHour = [eventHours floatValue];

    //some calculations

    if (self.imageLayer != nil) {
        //If image added earlier then this statement will remove it from superlayer which is your mapView
        [self.imageLayer removeFromSuperlayer];
    }

    self.imageLayer = [CALayer layer];
    self.imageLayer.bounds = CGRectMake(0, 0, 260, 260);
    CGPoint center = CGPointMake(150, 300);
    self.imageLayer.position = center;
    self.imageLayer.contents = (id) [UIImage imageNamed:@"angle.gif"].CGImage;
    self.imageLayer.affineTransform = CGAffineTransformMakeRotation(shadowDirection * 3.14/180);
    [[mapView layer] addSublayer:self.imageLayer];
}

如您所见,我正在访问我用于将图像添加到 MapView 的同一个 CALyer。为了能够坚持我在我的 .m 文件中创建了一个名为 imageLayer 的 属性。

你应该让图层从超级图层中移除它自己来移除它。

我尝试了不同的版本,在索引处插入,替换子图层等。图像正常工作的唯一版本是:[[mapView layer] setSublayers:nil];但是整个地图都消失了。

- (void)viewDidLoad {

[super viewDidLoad];

locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
//etc.
}

- (IBAction)selectTime:(id)sender {

NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
[outputFormatter setDateFormat:@"HH"];
NSString *eventHours = [outputFormatter stringFromDate:timePicker.date];
currentHour = [eventHours floatValue];

//some calculations

CALayer *layer = [CALayer layer];
layer.bounds = CGRectMake(0, 0, 260, 260);
CGPoint center = CGPointMake(150, 300);
layer.position = center;  
[[mapView layer] setSublayers:nil];  // map disappears, Image works :-)    
layer.contents = (id) [UIImage imageNamed:@"angle.gif"].CGImage;
layer.affineTransform = CGAffineTransformMakeRotation(shadowDirection * 3.14/180); 
[[mapView layer] addSublayer:layer];
}

- (IBAction)getLocation:(id)sender{

MKUserLocation *userLocation = mapView.userLocation;
MKCoordinateRegion region =
MKCoordinateRegionMakeWithDistance (userLocation.location.coordinate, 100, 100);
[mapView setRegion:region animated:YES];

longLabel = userLocation.coordinate.longitude;
latLabel = userLocation.coordinate.latitude;
}

我的错误是使用

CALayer *layer1 = [CALayer layer];

而不是

layer1 = [[CALayer alloc] init];

现在

[layer1 removeFromSuperlayer];

正在工作:-)