MKAnnotationView image with rotation

I would like to add UIView

in MKAnnotationView

as an image. But first I want to rotate by degrees UIView

and then add a new one UIImage

to UIView

. Could you help me? I've tried but the UIView never rotates.

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;

      

Method converts UIView to UIImage

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

    return image;
}

      

+3


source to share


1 answer


Remove this,

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

      

Add this,

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

      



Thereafter

UIImage *pinImage = [self imageFromUIView:pinView]; 

      

So the final code will be,

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;

      

+1


source







All Articles