2012-02-24 55 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

相关问题