objective-c - 带旋转的 MKAnnotationView 图像

标签 objective-c uiview rotation uiimage mkannotationview

我想将 UIView 添加到 MKAnnotationView 作为图像。但首先我想旋转 UIView,然后将新的 UIImage 添加到 UIVIew。你能帮助我吗?我试过了,但 UIView 从不旋转。

UIView *pinView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 63, 63)];
pinView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"IconMapPin"]];
pinView.transform = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(self.heading));

UIImage *pinImage = [self imageFromUIView:pinView];    
annotationView.image = pinImage;

UIView转UIImage的方法

- (UIImage *) imageFromUIView:(UIView*)view
{
    UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0);
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image;
}

最佳答案

删除这个,

pinView.transform = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(self.heading));

添加这个,

pinImage.transform = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(self.heading));

在此之后,

UIImage *pinImage = [self imageFromUIView:pinView]; 

所以最终代码将是,

UIView *pinView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 63, 63)];
pinView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"IconMapPin"]];


UIImage *pinImage = [self imageFromUIView:pinView]; 
pinImage.transform = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(self.heading));
annotationView.image = pinImage;

关于objective-c - 带旋转的 MKAnnotationView 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31334328/

相关文章:

ios - 当我使用带有按钮标题的下划线时,UIButton 标题闪烁

C++ 错误;我应该如何解释它的含义?

ios - 如何显示下面的UIView?

ios - 如何渲染离屏 IOS

objective-c - 在 NSWindow 中的不同容器上拖动 handle

iphone - 需要有关在 iphone 中捕获 UIImage 和 UIView 的帮助

objective-c - 当 UIView 框架更改时, View 内的 AVPlayer 层不会调整大小

c - 旋转 C 中任何整数的位

iOS swift Xcode 6 : CGAffineTransformRotate with auto-layout anchorpoint

javascript - 使用 THREE.js 方法计算相机偏航 Angular 的正确方法是什么?