【问题标题】:Clear Image on UIImageView subview of a MapView and draw new Image在 MapView 的 UIImageView 子视图上清除图像并绘制新图像
【发布时间】:2017-09-02 16:32:19
【问题描述】:

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

【问题讨论】:

    标签: ios uiimageview uiimage calayer


    【解决方案1】:

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

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

    【讨论】:

    • 为了测试,我在代码后面加上了 if 语句。发生的是,执行代码时未显示新图像。我必须把代码放在哪里?
    • @ggkey 请添加更多代码,说明您在哪里尝试使用此代码,或者您在哪里加载新图像,我会让您知道如何使用上述代码来实现所需的行为
    • 我在选择器函数中使用代码。使用选择器选择新角度,图像应旋转到新角度。在显示新图像之前,应删除具有旧角度的图像。谢谢
    • 为什么层删除...不起作用?任何想法?我是可可的新手,所以可能我不了解层的整个结构。感谢您提供更多建议。
    • 删除不起作用是什么意思?你尝试了什么?
    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      我的问题是使用

      CALayer *layer1 = [CALayer layer];
      

      而不是

      layer1 = [[CALayer alloc] init];
      

      现在

      [layer1 removeFromSuperlayer];
      

      正在工作:-)

      【讨论】:

        猜你喜欢
        • 2023-03-16
        • 1970-01-01
        • 2018-03-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-10-26
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多