Jak*_*kub 12 objective-c uiimageview mkannotation mkannotationview ios
我正在编写一个应用程序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;
}
Run Code Online (Sandbox Code Playgroud)
我原本以为我的针脚是红色的,但是保持黑色,只有标注(i)图标变成红色.

我试图设置self.view.tintColor和self.mapView.tintColor我的,ViewController但这也不起作用.如何将此引脚变为红色?
San*_*man 12
有一个比那些不涉及创建的更好的解决方案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
}
}
Run Code Online (Sandbox Code Playgroud)
像这样称呼它:
UIImage(named: "myImage")!
.imageWithRenderingMode(.AlwaysTemplate)
.colorized(UIColor.red())
Run Code Online (Sandbox Code Playgroud)
小智 5
我可以通过捕获到UIImage的UIView来完成这项工作:
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;
Run Code Online (Sandbox Code Playgroud)
与@ 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!
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4165 次 |
| 最近记录: |