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

发布于 2025-01-08 19:03:58 字数 216 浏览 0 评论 0原文

我一直在关注 WWDC 第 127 期 - 使用叠加层自定义地图。他们说您可以从应用程序包或网络获取图像以用作 MKOverlay。我想知道手机是否可以使用其 API 生成图像。

有人做过吗?您可以提供教程吗?

谢谢。

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

I've been following the WWDC Sesion 127- Customizing Maps With Overlays. They say you can get the image to use as a MKOverlay either from the apps bundle or from the network. I would like to know if the image can be generated by the phone using it's APIs.

Has anyone done it? Can you maybe provide a tutorial?

Thanks.

[EDIT]
I would also actually need a tutorial on how to add that image, created by the phone or downloadded from the network, to the map.
All I see is the WWDC demo but you need the ticket to the conference, wich I obviously dont have.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黯然 2025-01-15 19:03:58

下面是一些使用 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);
}

Here is some code for drawing using Quartz into an UIImage. Hope it is helpful, and you can get the idea

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文