问题描述
我正在为 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.tintColor
和 self.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图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!