将 TintColor 设置为 MKAnnotationView 图像

set TintColor to MKAnnotationView image(将 TintColor 设置为 MKAnnotationView 图像)

本文介绍了将 TintColor 设置为 MKAnnotationView 图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm writing an application for iOS 7.0+ and I wanted to use new feature witch is: imageWithRenderingMode. I have a map annotation with this code:

-(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) 图标变为红色.

I was expected that my pins will be red, but stays black, only the callout (i) icon turns red.

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

I was trying to set self.view.tintColor and self.mapView.tintColor in my ViewController but this not working either. How to turns this pins red?

推荐答案

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

There's a better solution than those proposed that doesn't involve creating a UIImageView.

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

This Swift code will create a colored version of your 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())

这篇关于将 TintColor 设置为 MKAnnotationView 图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:将 TintColor 设置为 MKAnnotationView 图像

基础教程推荐