2015-07-10 59 views
3

我想添加UIViewMKAnnotationView作为图像。但首先我要旋转UIView,然后添加新的UIImageUIVIew。你可以帮我吗?我试过但UIView从不旋转。MKAnnotationView旋转的图像

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

删除此,

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;