我一直在关注 WWDC 第 127 届会议-使用叠加层自定义地图。他们说您可以从应用程序包或网络获取图像以用作 MKOverlay。我想知道图像是否可以由手机使用它的 API 生成。
有人做过吗?你能提供一个教程吗?
谢谢。
[编辑] 实际上,我还需要一个关于如何将由手机创建或从网络下载的图像添加到地图的教程。我所看到的只是 WWDC 演示,但您需要参加会议的门票,而我显然没有。
我一直在关注 WWDC 第 127 届会议-使用叠加层自定义地图。他们说您可以从应用程序包或网络获取图像以用作 MKOverlay。我想知道图像是否可以由手机使用它的 API 生成。
有人做过吗?你能提供一个教程吗?
谢谢。
[编辑] 实际上,我还需要一个关于如何将由手机创建或从网络下载的图像添加到地图的教程。我所看到的只是 WWDC 演示,但您需要参加会议的门票,而我显然没有。
这是一些使用 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);
}