带旋转的MKAnnotationView图像

时间:2015-07-10 06:54:37

标签: 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;
}

1 个答案:

答案 0 :(得分: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;