set TintColor to MKAnnotationView image(将 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.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 图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:将 TintColor 设置为 MKAnnotationView 图像
基础教程推荐
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
