Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/94.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 生成没有MKMapView的MKPolyline的小缩略图图像?_Ios_Objective C_Uiimage_Mkmapview_Mkoverlay - Fatal编程技术网

Ios 生成没有MKMapView的MKPolyline的小缩略图图像?

Ios 生成没有MKMapView的MKPolyline的小缩略图图像?,ios,objective-c,uiimage,mkmapview,mkoverlay,Ios,Objective C,Uiimage,Mkmapview,Mkoverlay,我们的应用程序包含几个多段线边界,所有这些边界都创建一个闭合的多边形。这些多边形主要显示为MKMapView上的MKOverlay,但我正在寻找一种解决方案,将这些多边形显示为小缩略图,使其不在MKMapView上可见,而是显示为标准UIImage或UIImageView 为了清楚起见,我希望这些小缩略图只显示为具有笔划颜色和填充颜色但没有任何地图背景的小形状 谁能帮我做这个吗?给你 + (UIImage *)imageNamed:(NSString *)name withColor:(UICo

我们的应用程序包含几个多段线边界,所有这些边界都创建一个闭合的多边形。这些多边形主要显示为MKMapView上的MKOverlay,但我正在寻找一种解决方案,将这些多边形显示为小缩略图,使其不在MKMapView上可见,而是显示为标准UIImage或UIImageView

为了清楚起见,我希望这些小缩略图只显示为具有笔划颜色和填充颜色但没有任何地图背景的小形状

谁能帮我做这个吗?

给你

+ (UIImage *)imageNamed:(NSString *)name withColor:(UIColor *)color{
    // load the image
    UIImage *img = [UIImage imageNamed:name];

    // begin a new image context, to draw our colored image onto
    UIGraphicsBeginImageContext(img.size);

    // get a reference to that context we created
    CGContextRef context = UIGraphicsGetCurrentContext();

    // set the fill color
    [color setFill];

    // translate/flip the graphics context (for transforming from CG* coords to UI* coords
    CGContextTranslateCTM(context, 0, img.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);

    // set the blend mode to color burn, and the original image
    CGContextSetBlendMode(context, kCGBlendModeColorBurn);
    CGRect rect = CGRectMake(0, 0, img.size.width, img.size.height);
    CGContextDrawImage(context, rect, img.CGImage);

    // set a mask that matches the shape of the image, then draw (color burn) a colored rectangle
    CGContextClipToMask(context, rect, img.CGImage);
    CGContextAddRect(context, rect);
    CGContextDrawPath(context,kCGPathFill);

    // generate a new UIImage from the graphics context we drew onto
    UIImage *coloredImg = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    //return the color-burned image
    return coloredImg;
}
请查看这篇文章的详细描述。

给你

+ (UIImage *)imageNamed:(NSString *)name withColor:(UIColor *)color{
    // load the image
    UIImage *img = [UIImage imageNamed:name];

    // begin a new image context, to draw our colored image onto
    UIGraphicsBeginImageContext(img.size);

    // get a reference to that context we created
    CGContextRef context = UIGraphicsGetCurrentContext();

    // set the fill color
    [color setFill];

    // translate/flip the graphics context (for transforming from CG* coords to UI* coords
    CGContextTranslateCTM(context, 0, img.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);

    // set the blend mode to color burn, and the original image
    CGContextSetBlendMode(context, kCGBlendModeColorBurn);
    CGRect rect = CGRectMake(0, 0, img.size.width, img.size.height);
    CGContextDrawImage(context, rect, img.CGImage);

    // set a mask that matches the shape of the image, then draw (color burn) a colored rectangle
    CGContextClipToMask(context, rect, img.CGImage);
    CGContextAddRect(context, rect);
    CGContextDrawPath(context,kCGPathFill);

    // generate a new UIImage from the graphics context we drew onto
    UIImage *coloredImg = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    //return the color-burned image
    return coloredImg;
}

请查看这篇文章的详细描述。

我必须在我自己的应用程序中执行完全相同的操作。下面是我的解决方案:我生成一个表示路径形状的
UIView
。在您的情况下,路径是一条
MKPolyline

这是我的密码:

+ (UIView *)createShapeForGPX:(GPX *)gpx
                withFrameSize:(CGSize)frameSize
                    lineColor:(UIColor *)lineColor {

    // Array of coordinates (Adapt this code with your coordinates)
    // Note : in my case I have a double loops because points are in paths 
    // and I can have many paths for one route. So I concact all points 
    // into one array to simplify the code for your case. If you also have
    // many paths, you have to change a little bit next code.
    NSMutableArray<NSValue *> *dataPoints = [NSMutableArray new];
    for (NSArray *path in gpx.paths) {
        for (NSDictionary *point in path) {
            double latitude = [point[@"latitude"] doubleValue];
            double longitude = [point[@"longitude"] doubleValue];
            [dataPoints addObject:[NSValue valueWithCGPoint:CGPointMake(longitude, latitude)]];
        }
    }

    // Graph bounds (You need to calculate topRightCoordinate and bottomleftCoordinate. You can do it in previous for loop)
    double lngBorder = gpx.topRightCoordinate.longitude - gpx.bottomLeftCoordinate.longitude;
    double latBorder = gpx.topRightCoordinate.latitude - gpx.bottomLeftCoordinate.latitude;

    double middleLng = gpx.bottomLeftCoordinate.longitude + (lngBorder / 2.f);
    double middleLat = gpx.bottomLeftCoordinate.latitude + (latBorder / 2.f);

    double boundLength = MAX(lngBorder, latBorder);

    // *** Drawing ***
    CGFloat margin = 4.f;
    UIView *graph = [UIView new];
    graph.frame = CGRectMake(0, 0, frameSize.width - margin, frameSize.height - margin);

    CAShapeLayer *line = [CAShapeLayer layer];
    UIBezierPath *linePath = [UIBezierPath bezierPath];

    float xAxisMin = middleLng - (boundLength / 2.f);
    float xAxisMax = middleLng + (boundLength / 2.f);
    float yAxisMin = middleLat - (boundLength / 2.f);
    float yAxisMax = middleLat + (boundLength / 2.f);

    int i = 0;
    while (i < dataPoints.count) {

        CGPoint point = [dataPoints[i] CGPointValue];

        float xRatio = 1.0-((xAxisMax-point.x)/(xAxisMax-xAxisMin));
        float yRatio = 1.0-((yAxisMax-point.y)/(yAxisMax-yAxisMin));

        float x = xRatio*(frameSize.width - margin / 2);
        float y = (1.0-yRatio)*(frameSize.height - margin);

        if (i == 0) {
            [linePath moveToPoint:CGPointMake(x, y)];
        } else {
            [linePath addLineToPoint:CGPointMake(x, y)];
        }
        i++;
    }

    // Line
    line.lineWidth = 0.8;
    line.path = linePath.CGPath;
    line.fillColor = [[UIColor clearColor] CGColor];
    line.strokeColor = [lineColor CGColor];
    [graph.layer addSublayer:line];

    graph.backgroundColor = [UIColor clearColor];

    // Final view (add margins)
    UIView *finalView = [UIView new];
    finalView.backgroundColor = [UIColor clearColor];
    finalView.frame = CGRectMake(0, 0, frameSize.width, frameSize.height);
    graph.center = CGPointMake(CGRectGetMidX(finalView.bounds), CGRectGetMidY(finalView.bounds));
    [finalView addSubview:graph];

    return finalView;
}
+(UIView*)createShapeForGPX:(GPX*)GPX
withFrameSize:(CGSize)frameSize
lineColor:(UIColor*)lineColor{
//坐标数组(使用坐标调整此代码)
//注意:在我的例子中,我有一个双循环,因为点在路径中
//一条路线可以有很多条路,所以我把所有的点都连接起来
//合并到一个数组中,以简化案例的代码
//很多路径,您必须在下一个代码中更改一点。
NSMUTABLEARRY*数据点=[NSMUTABLEARRY new];
for(gpx.path中的NSArray*路径){
用于(NSDictionary*路径中的点){
双纬度=[点[@“纬度”]双值];
双经度=[点[@“经度”]双值];
[dataPoints addObject:[NSValue valueWithCGPoint:CGPointMake(经度、纬度)];
}
}
//图形边界(您需要计算topRightCoordinate和bottomleftCoordinate。您可以在前面的for循环中进行计算)
双lngBorder=gpx.topRightCoordinate.longitude-gpx.bottomLeftCoordinate.longitude;
双latBorder=gpx.topRightCoordinate.latitude-gpx.bottomLeftCoordinate.latitude;
double middleLng=gpx.bottomLeftCoordinate.longitude+(lngBorder/2.f);
双中间纬度=gpx.bottomleftcoordination.latitude+(纬度边界/2.f);
双边界长度=最大值(lngBorder,latBorder);
//***图纸***
cg浮动保证金=4.f;
UIView*图形=[UIView新建];
graph.frame=CGRectMake(0,0,frameSize.width-margin,frameSize.height-margin);
CAShapeLayer*行=[CAShapeLayer层];
UIBezierPath*linePath=[UIBezierPath bezierPath];
float xAxisMin=middleLng-(边界长度/2.f);
float xAxisMax=middleLng+(边界长度/2.f);
float yAxisMin=middleLat-(boundLength/2.f);
float yAxisMax=middleLat+(boundLength/2.f);
int i=0;
而(i
在我的例子中,
GPX
类包含很少的值:
-
NSArray*路径:包含所有路径的所有点。在您的情况下,我认为这是您的
MKPolyline

-
topRightCoordinate
bottomLeftCoordinate
:两个
CLLocationCoordinate2D
表示路径的右上和左下虚拟坐标(您还必须计算它们)

您可以这样调用此方法:
UIView*shape=[YOURCLASS createShapeForGPX:gpx with frameSize:CGSizeMake(32,32)lineColor:[UIColor blackColor]

此解决方案基于此问题,该问题给出了从点绘制图形的解决方案

也许所有这些代码对您都没有用处(比如边距),但它应该可以帮助您找到自己的解决方案

以下是它在我的应用程序(在
UITableView
中)中的显示方式:


我必须在自己的应用程序中执行完全相同的操作。下面是我的解决方案:我生成一个表示路径形状的
UIView
。在您的情况下,路径是一条
MKPolyline

这是我的密码:

+ (UIView *)createShapeForGPX:(GPX *)gpx
                withFrameSize:(CGSize)frameSize
                    lineColor:(UIColor *)lineColor {

    // Array of coordinates (Adapt this code with your coordinates)
    // Note : in my case I have a double loops because points are in paths 
    // and I can have many paths for one route. So I concact all points 
    // into one array to simplify the code for your case. If you also have
    // many paths, you have to change a little bit next code.
    NSMutableArray<NSValue *> *dataPoints = [NSMutableArray new];
    for (NSArray *path in gpx.paths) {
        for (NSDictionary *point in path) {
            double latitude = [point[@"latitude"] doubleValue];
            double longitude = [point[@"longitude"] doubleValue];
            [dataPoints addObject:[NSValue valueWithCGPoint:CGPointMake(longitude, latitude)]];
        }
    }

    // Graph bounds (You need to calculate topRightCoordinate and bottomleftCoordinate. You can do it in previous for loop)
    double lngBorder = gpx.topRightCoordinate.longitude - gpx.bottomLeftCoordinate.longitude;
    double latBorder = gpx.topRightCoordinate.latitude - gpx.bottomLeftCoordinate.latitude;

    double middleLng = gpx.bottomLeftCoordinate.longitude + (lngBorder / 2.f);
    double middleLat = gpx.bottomLeftCoordinate.latitude + (latBorder / 2.f);

    double boundLength = MAX(lngBorder, latBorder);

    // *** Drawing ***
    CGFloat margin = 4.f;
    UIView *graph = [UIView new];
    graph.frame = CGRectMake(0, 0, frameSize.width - margin, frameSize.height - margin);

    CAShapeLayer *line = [CAShapeLayer layer];
    UIBezierPath *linePath = [UIBezierPath bezierPath];

    float xAxisMin = middleLng - (boundLength / 2.f);
    float xAxisMax = middleLng + (boundLength / 2.f);
    float yAxisMin = middleLat - (boundLength / 2.f);
    float yAxisMax = middleLat + (boundLength / 2.f);

    int i = 0;
    while (i < dataPoints.count) {

        CGPoint point = [dataPoints[i] CGPointValue];

        float xRatio = 1.0-((xAxisMax-point.x)/(xAxisMax-xAxisMin));
        float yRatio = 1.0-((yAxisMax-point.y)/(yAxisMax-yAxisMin));

        float x = xRatio*(frameSize.width - margin / 2);
        float y = (1.0-yRatio)*(frameSize.height - margin);

        if (i == 0) {
            [linePath moveToPoint:CGPointMake(x, y)];
        } else {
            [linePath addLineToPoint:CGPointMake(x, y)];
        }
        i++;
    }

    // Line
    line.lineWidth = 0.8;
    line.path = linePath.CGPath;
    line.fillColor = [[UIColor clearColor] CGColor];
    line.strokeColor = [lineColor CGColor];
    [graph.layer addSublayer:line];

    graph.backgroundColor = [UIColor clearColor];

    // Final view (add margins)
    UIView *finalView = [UIView new];
    finalView.backgroundColor = [UIColor clearColor];
    finalView.frame = CGRectMake(0, 0, frameSize.width, frameSize.height);
    graph.center = CGPointMake(CGRectGetMidX(finalView.bounds), CGRectGetMidY(finalView.bounds));
    [finalView addSubview:graph];

    return finalView;
}
+(UIView*)createShapeForGPX:(GPX*)GPX
withFrameSize:(CGSize)frameSize
lineColor:(UIColor*)lineColor{
//坐标数组(使用坐标调整此代码)
//注意:在我的例子中,我有一个双循环,因为点在路径中
//一条路线可以有很多条路,所以我把所有的点都连接起来
//合并到一个数组中,以简化案例的代码
//很多路径,您必须在下一个代码中更改一点。
NSMUTABLEARRY*数据点=[NSMUTABLEARRY new];
for(gpx.path中的NSArray*路径){
用于(NSDictionary*路径中的点){
双纬度=[点[@“纬度”]双值];
双经度=[点[@“经度”]双值];
[dataPoints addObject:[NSValue valueWithCGPoint:CGPointMake(经度、纬度)];
}
}
//图界