MKAnnotationView 图像旋转

3

我想将 UIView 作为图像添加到 MKAnnotationView 中,但首先我想旋转 UIView 并添加新的 UIImageUIVIew 中。你能帮我吗?我尝试过了,但是 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;
}
1个回答

1

删除这个

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;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接