【问题标题】:set TintColor to MKAnnotationView image将 TintColor 设置为 MKAnnotationView 图像
【发布时间】:2014-05-06 20:20:33
【问题描述】:

我正在为iOS 7.0+ 编写应用程序,我想使用新功能,女巫是:imageWithRenderingMode。我有一个带有此代码的地图注释:

-(MKAnnotationView*)annotationView {
    MKAnnotationView *annotationView = [[MKAnnotationView alloc] initWithAnnotation:self reuseIdentifier:@"AnnotationIdentifier"];
    annotationView.enabled = YES;
    annotationView.canShowCallout = YES;
    annotationView.image = [[UIImage imageNamed:@"star"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
    annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    annotationView.tintColor = [UIColor redColor];
    return annotationView;
}

我预计我的图钉会是红色的,但保持黑色,只有标注 (i) 图标变为红色。

我试图在我的ViewController 中设置self.view.tintColorself.mapView.tintColor,但这也不起作用。如何将这个引脚变成红色?

【问题讨论】:

    标签: ios objective-c uiimageview mkannotation mkannotationview


    【解决方案1】:

    有一个比不涉及创建UIImageView 的建议更好的解决方案。

    此 Swift 代码将创建您的 UIImage 的彩色版本。

    extension UIImage {
    
        func colorized(color : UIColor) -> UIImage {
            let rect = CGRectMake(0, 0, self.size.width, self.size.height);
            UIGraphicsBeginImageContextWithOptions(rect.size, false, 0.0);
            let context = UIGraphicsGetCurrentContext();
            CGContextSetBlendMode(context, .Multiply)
            CGContextDrawImage(context, rect, self.CGImage)
            CGContextClipToMask(context, rect, self.CGImage)
            CGContextSetFillColorWithColor(context, color.CGColor)
            CGContextFillRect(context, rect)
            let colorizedImage = UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsEndImageContext();
            return colorizedImage
        }
    }
    

    这样称呼它:

    UIImage(named: "myImage")!
        .imageWithRenderingMode(.AlwaysTemplate)
        .colorized(UIColor.red())
    

    【讨论】:

    • 谢谢你。我的图像被翻转了,我在CGContextDrawImage(context, rect, self.CGImage) 之前添加了CGContextTranslateCTM(context, 0, self.size.height); CGContextScaleCTM(context, 1.0, -1.0) 来解决这个问题。
    • 什么是混合模式?我必须删除它才能获得正确的颜色
    • @Vinzzz 我相信您使用的混合模式将取决于源图像的颜色。就我而言,我相信我的图像是白色的,但您可以尝试其他混合模式(甚至是默认模式),看看它如何影响您的显示。
    • swift 4 更新:'annotationView?.image = image?.withRenderingMode(.alwaysTemplate).colorized(color: colour)'
    • 大多数人可能想要混合模式.copy,尤其是在使用系统图像时
    【解决方案2】:

    我可以通过capturing an UIView to UIImage 完成这项工作:

    UIImage *pin = [UIImage imageNamed:@"pin"];
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, pin.size.width, pin.size.height)];
    imageView.image = [pin imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
    imageView.tintColor = [UIColor grayColor]; // set the desired color
    
    // now the magic...
    UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, imageView.opaque, 0.0);
    [imageView.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    annotationView.image = img;
    

    【讨论】:

    • 不适合我。 UIGraphicsGetCurrentContext() 返回零。
    【解决方案3】:

    多么令人沮丧。我这样解决了(没有其他属性配置):

    -(MKAnnotationView*)annotationView {
        MKAnnotationView *annotationView = [[MKAnnotationView alloc] initWithAnnotation: self reuseIdentifier: @"AnnotationIdentifier"];
        UIImage *image = [[UIImage imageNamed: @"star"] imageWithRenderingMode: UIImageRenderingModeAlwaysTemplate];
        UIImageView *screwYouApple = [[UIImageView alloc] initWithImage: image];
        screwYouApple.tintColor = [UIColor redColor];
        [annotationView addSubview: screwYouApple];
        return annotationView;
    }
    

    基本上,我删除了annotationViewimage 属性,并使用正确着色的UIImageView 作为注释的subview

    变量命名有助于宣泄体验。

    【讨论】:

    • 看起来这样会禁用呼出。
    【解决方案4】:

    与@sandy-chapman 相同,但在 Swift 3 中。我的图像被翻转了,所以我再次将它们翻转回来。

    extension UIImage {
    
      func colorized(color : UIColor) -> UIImage {
    
        let rect = CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height)
    
        UIGraphicsBeginImageContextWithOptions(rect.size, false, 0.0)
        if let context = UIGraphicsGetCurrentContext() {
            context.setBlendMode(.multiply)
            context.translateBy(x: 0, y: self.size.height)
            context.scaleBy(x: 1.0, y: -1.0)
            context.draw(self.cgImage!, in: rect)
            context.clip(to: rect, mask: self.cgImage!)
            context.setFillColor(color.cgColor)
            context.fill(rect)
        }
    
        let colorizedImage = UIGraphicsGetImageFromCurrentImageContext()
    
        UIGraphicsEndImageContext()
        return colorizedImage!
    
      }
    }
    

    【讨论】:

      【解决方案5】:

      意识到我玩游戏已经很晚了,但是我一直在寻找与您相同的东西,但发现这很有用。

      使用MKMarkerAnnotationView,它有一个属性markerTintColor

      您不会获得自定义图钉图片,但它看起来很像您在问题中使用的标记(请参阅我的附件图片)。

      以下是我如何使用它的 sn-p:

        // Somewhere else 
        mapView.register(MKMarkerAnnotationView.self, forAnnotationViewWithReuseIdentifier: MKMarkerAnnotationView.self.description())  
      
        func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
              guard let companyAnnotation = annotation as? CompanyAnnotation else { return nil }
              let view = MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: MKMarkerAnnotationView.self.description())
              view.markerTintColor = .colorFor(category: companyAnnotation.company.category)
              return view
         }
      

      【讨论】:

        【解决方案6】:

        这对我有用:

        extension MKAnnotationView {
        
            public func set(image: UIImage, with color : UIColor) {
                let view = UIImageView(image: image.withRenderingMode(.alwaysTemplate))
                view.tintColor = color
                UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.isOpaque, 0.0)
                guard let graphicsContext = UIGraphicsGetCurrentContext() else { return }
                view.layer.render(in: graphicsContext)
                let image = UIGraphicsGetImageFromCurrentImageContext()
                UIGraphicsEndImageContext()
                self.image = image
            }
            
        }
        

        (基于@Marcio Fonseca 的回答)

        【讨论】:

          【解决方案7】:

          您似乎根本无法使用tintColor 完成此操作。 MKAnnotationView 不像 UIImageView 那样使用色调颜色进行图像渲染。您需要自己编写某种方法来绘制正确颜色的图像,或者提供具有所需颜色的图像。

          【讨论】:

          • 哈,这很奇怪,因为MKAnnotationView image property 是普通的imageView,但我很确定这是不可能的,但不知道为什么......
          • MKAnnotationUIView 的子类,而不是UIImageView。它的image 属性期待UIImage 的实例。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-06-09
          • 2018-02-19
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多