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

时间:2017-09-02 16:32:20

标签: ios uiimageview uiimage calayer

我在子视图中将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];

3 个答案:

答案 0 :(得分:0)

从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的属性。

您应该要求图层从超级图层中移除它以将其删除。

答案 1 :(得分:0)

我尝试了不同的版本,插入索引,替换子图层等。图像工作的唯一版本应该是:[[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;
}

答案 2 :(得分:0)

我的案子的错误是,使用

CALayer *layer1 = [CALayer layer];

而不是

layer1 = [[CALayer alloc] init];

现在

[layer1 removeFromSuperlayer];

正在运作: - )