2013-06-13 24 views
5

我有覆蓋的路線放置在地圖上。我必須調整其旋轉角度,以便與底層地圖視圖完全重疊。我怎樣才能做到這一點?我發現了旋轉的角度。我如何旋轉覆蓋視圖?到目前爲止,我已添加疊加層,但無法使用變換進行旋轉。我注意到,當我改變角度時,圖像不旋轉,而是沿x軸滑動(側向)。MKOverlayView要旋轉

這裏是我試過

- (void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)context 
{ 
    NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"routeImage" ofType:@"png"]; 
    UIImage *image = [[UIImage alloc] initWithContentsOfFile:imagePath]; 

    MKMapRect theMapRect = self.overlay.boundingMapRect; 
    CGRect theRect = [self rectForMapRect:theMapRect]; 

    CGAffineTransform transform = CGAffineTransformMakeRotation((-35.88)/180.0 * M_PI); 
    [self setTransform:transform]; 

    CGContextSaveGState(context); 

    CGContextScaleCTM(context, 1.0, -1.0); 
    CGContextTranslateCTM(context, 0.0, -theRect.size.height); 
    CGContextDrawImage(context, theRect,image.CGImage); 
    CGContextRestoreGState(context); 
} 

代碼這是我得到

enter image description here

我有一個解決方法中,我使用GIMP手動旋轉圖像,並把它。但我需要它來定製。

這是使用GIMP變換了image和當放置在圖作爲覆蓋

,這是當我使用變換[注旋轉上下文:在旋轉的同時圖像被旋轉,但不是在精確的位置見上下文image。同樣在縮放圖像中斷]

這是我做過什麼

 //Hayl-Road-Final.png is the actual image without transformation using GIMP 

     UIImage *image = [[UIImage imageNamed:@"Hayl-Road-Final.png"] retain]; 
     CGImageRef imageReference = image.CGImage; 


     MKMapRect theMapRect = [self.overlay boundingMapRect]; 
     CGRect theRect   = [self rectForMapRect:theMapRect]; 


     // We need to flip and reposition the image here 
     CGContextScaleCTM(ctx, 1.0, -1.0); 
     CGContextTranslateCTM(ctx, 0.0, -theRect.size.height); 

     CGContextRotateCTM(ctx,-35.88); 
     //drawing the image to the context 
     CGContextDrawImage(ctx, theRect, imageReference); 
+0

如何是你的初始化代碼,或者你正在將轉換應用於「自我」的視圖是什麼?你嘗試旋轉背景嗎? – Lukas

+0

@Lukas:它是自我引用的覆蓋視圖。我甚至嘗試旋轉背景。發生了什麼事是,當放大圖像中斷時。有沒有其他方法可以實現這一點?我覺得有一種方法可以做,但我嘗試過的所有方法都可以通過縮放或準確位置的圖像實現旋轉。不是在一起。 – Meera

+0

@MeeraJPai你可以提供一個圖像旋轉你想要旋轉的方式嗎?我知道你可能不得不使用你的解決方法來獲得你想要的這個實例。 –

回答

0

嘗試加入這個方法:

- (UIImage *) rotate: (UIImage *) image angle:(double) 
{ 
    //double angle = 20; 
    CGSize s = {image.size.width, image.size.height}; 
    UIGraphicsBeginImageContext(s); 
    CGContextRef ctx = UIGraphicsGetCurrentContext(); 
    CGContextTranslateCTM(ctx, 0,image.size.height); 
    CGContextScaleCTM(ctx, 1.0, -1.0); 

    CGContextRotateCTM(ctx, 2*M_PI*angle/360); 
    CGContextDrawImage(ctx,CGRectMake(0,0,image.size.width, image.size.height),image.CGImage); 
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    return newImage; 

} 
0
From your code, you change order of two line. It will be ok: 

CGContextScaleCTM(ctx, 1.0, -1.0); 
CGContextRotateCTM(ctx,-35.88); 
CGContextTranslateCTM(ctx, 0.0, -theRect.size.height); 
CGContextDrawImage(ctx, theRect, imageReference); 

這是我正確的,請嘗試