Patrik Dendis
Patrik Dendis

Reputation: 313

MKAnnotationView image with rotation

i would like add UIView to MKAnnotationView as image. But first I wanna rotation by degrees UIView and then add new UIImage to UIVIew. Can you help me? I tried but UIView never rotate.

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 convert 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;
}

Upvotes: 3

Views: 215

Answers (1)

Samraan Khaan
Samraan Khaan

Reputation: 480

Remove this,

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

Add this,

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

After this,

UIImage *pinImage = [self imageFromUIView:pinView]; 

So 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;

Upvotes: 1

Related Questions