【问题标题】:MKAnnotationView image with rotationMKAnnotationView 图像旋转
【发布时间】:2015-07-10 06:54:37
【问题描述】:

我想将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;
}

【问题讨论】:

    标签: objective-c uiview rotation uiimage mkannotationview


    【解决方案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;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多