2012-02-24 34 views
0

我一直在關注WWDC Sesion 127-使用疊加層定製地圖。他們說你可以從應用程序包或網絡中獲取圖像作爲MKOverlay使用。我想知道是否可以通過手機使用API​​生成圖像。iphone - 你可以添加一個手機創建的圖像到MKOverlay?

有沒有人完成它?你可以提供一個教程嗎?

謝謝。

[編輯] 我實際上還需要一個關於如何將該圖像(由手機創建或從網絡下載)添加到地圖的教程。 我看到的只是WWDC演示,但你需要參加會議的票,我很明顯沒有。

+0

什麼叫「如果可以通過使用它的API的電話產生的圖像」是什麼意思?什麼是你的形象的來源?文件(.jpg,.png等)還是NSData? – Mutix 2012-02-24 09:52:34

+0

使用CG庫 – subharb 2012-02-24 10:11:56

回答

1

下面是使用Quartz繪製到UIImage中的一些代碼。希望這是有幫助的,你可以得到的想法

void mainFunction() { 
    // background size 
    CGSize sizeBack = CGSizeMake(layerFrame.size.width*scaledFactor,layerFrame.size.height*scaledFactor); 
    UIGraphicsBeginImageContext(sizeBack); 
    CGContextRef context = UIGraphicsGetCurrentContext(); 

    CGContextSetRGBFillColor(context, 0., 0., 0., 1.); 
    drawBorder(context, scaledRect); 

    UIImage *img = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    if (destinationLayer) { 
     [destinationLayer setContents:(id)img.CGImage]; 
    } 
} 

CGPathRef createRoundedRectanglePath(CGRect mainRect, CGFloat roundRatio) { 

    roundRatio = fabs(roundRatio); 
    roundRatio = MIN(1, roundRatio); 
    CGFloat offset = roundRatio * MIN(mainRect.size.width, mainRect.size.height)/2; 

    CGPoint c1 = CGPointMake(0,0); 
    CGPoint c2 = CGPointMake(mainRect.size.width,0); 
    CGPoint c3 = CGPointMake(mainRect.size.width,mainRect.size.height); 
    CGPoint c4 = CGPointMake(0,mainRect.size.height); 

    CGPoint pA = CGPointMake(c1.x + offset, c1.y); 
    CGPoint pB = CGPointMake(c2.x - offset, c2.y); 
    CGPoint pC = CGPointMake(c2.x, c2.y + offset); 
    CGPoint pD = CGPointMake(c3.x, c3.y - offset); 
    CGPoint pE = CGPointMake(c3.x - offset, c3.y); 
    CGPoint pF = CGPointMake(c4.x + offset, c4.y); 
    CGPoint pG = CGPointMake(c4.x, c4.y - offset); 
    CGPoint pH = CGPointMake(c1.x, c1.y + offset); 


    CGMutablePathRef result = CGPathCreateMutable(); 
    CGPathMoveToPoint(result, NULL, pA.x, pA.y); 

    CGPathAddLineToPoint(result, NULL, pB.x, pB.y); 
    CGPathAddQuadCurveToPoint(result, NULL, c2.x, c2.y, pC.x, pC.y); 

    CGPathAddLineToPoint(result, NULL, pD.x, pD.y); 
    CGPathAddQuadCurveToPoint(result, NULL, c3.x, c3.y, pE.x, pE.y); 

    CGPathAddLineToPoint(result, NULL, pF.x, pF.y); 
    CGPathAddQuadCurveToPoint(result, NULL, c4.x, c4.y, pG.x, pG.y); 

    CGPathAddLineToPoint(result, NULL, pH.x, pH.y); 
    CGPathAddQuadCurveToPoint(result, NULL, c1.x, c1.y, pA.x, pA.y); 

    CGPathCloseSubpath(result); 

    CGPathRef path = CGPathCreateCopy(result); 
    CGPathRelease(result); 
    return path; 
} 

void drawBorder(CGContextRef context, CGRect rect) { 
    CGContextSaveGState(context); 
    /* Outside Path */ 
    CGPathRef thePath = createRoundedRectanglePath(rect, 0.2); 

    /* Fill with Gradient Color */ 
    CGFloat locations[] = { 0.0, 1.0 }; 
    CGColorRef startColor = [UIColor colorWithRed:1. 
              green:1. 
              blue:1. 
              alpha:1.].CGColor; 
    CGColorRef endColor = [UIColor colorWithRed:208./255.           
              green:208./255. 
              blue:208./255. 
              alpha:1.].CGColor; 


    NSArray *colors = [NSArray arrayWithObjects:(id)startColor, (id)endColor, nil]; 

    CGPoint startPoint = CGPointMake(CGRectGetMidX(rect), 0); 
    CGPoint endPoint = CGPointMake(CGRectGetMidX(rect), rect.size.height); 

    CGContextAddPath(context, thePath); 
    CGContextClip(context); 
    drawLinearGradient(context, startPoint, endPoint, locations, (CFArrayRef*)colors); 

    /* Stroke path */ 
    CGPathRelease(thePath); 
    thePath = createRoundedRectanglePath(rect, 0.2); 
    CGContextSetStrokeColor(context, CGColorGetComponents([UIColor colorWithRed:0 green:0 blue:1. alpha:1.].CGColor)); 
    CGContextAddPath(context, thePath); 
    CGContextSetLineWidth(context, 1.); 
    CGContextDrawPath(context, kCGPathStroke); 
    CGContextRestoreGState(context); 
    CGPathRelease(thePath); 
} 
+0

謝謝,但是還想知道的是,如果我使用Quartz製作該圖像,那麼我能夠將它作爲MKOverlay添加到地圖嗎? – subharb 2012-02-24 11:44:52

+1

是的,這是可能的,你需要子類MKOverlayView,然後重寫這個方法「drawMapRect:zoomScale:inContext:」。在這種方法中,您可以使用石英直接繪製您的內容。所以你甚至不需要製作UIImage! – Ganzolo 2012-02-24 12:08:58

+0

你碰巧知道一個教程,我可以做到這一點?我相當新的MKOverlays和石英,所以這一切都像我的巫術... – subharb 2012-02-24 13:51:16

相關問題