iphone - 您可以将手机创建的图像添加到MKOverlay吗?

时间:2012-02-24 09:41:34

标签: objective-c mkoverlay

我一直在关注WWDC Sesion 127-使用叠加层自定义地图。他们说你可以从应用程序包或网络中将图像用作MKOverlay。我想知道手机是否可以使用它的API生成图像。

有人做过吗?你能提供一个教程吗?

感谢。

[编辑] 我实际上还需要一个关于如何将手机创建或从网络下载的图像添加到地图的教程。 我所看到的只是WWDC演示,但你需要参加会议的门票,我显然没有。

1 个答案:

答案 0 :(得分: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);
}