最近在做一款激光扫地机地图,期间遇到了很多问题,在此做个记录,基本效果如下:
主要涉及到:
1、十六进制点阵转位图。
这部是绘制地图的关键,主要实现如下:
NSData *newData = [NSData dataWithBytes:dstBuffer length:decompressLen];
Byte *newByte = (Byte *)newData.bytes;
char* rgba = (char*)malloc(width*height*4);
int offset=0;
for(int i = 0; i < height; ++i)
{
for (int j = 0; j < width; j++)
{
int dotValue = newByte[(height-1-i)*width+j];
if (dotValue == 127) {//地图背景
CGFloat cmp[3];
[self dc_getRGBComponents:cmp forColor:self->_bgColor];
rgba[4*offset] = cmp[0] * kColor;
rgba[4*offset+1] = cmp[1] * kColor;
rgba[4*offset+2] = cmp[2] * kColor;
rgba[4*offset+3] = 0;
} else if (dotValue == 0) { //地图边界
CGFloat cmp[3];
[self dc_getRGBComponents:cmp forColor:self->_boundaryColor];
rgba[4*offset] = cmp[0] * kColor;
rgba[4*offset+1] = cmp[1] * kColor;
rgba[4*offset+2] = cmp[2] * kColor;
rgba[4*offset+3] = 0;
} else { //地图内部
if (dotValue == 255) {
CGFloat cmp[3];
[self dc_getRGBComponents:cmp forColor:self->_exploredAreaColor];
rgba[4*offset] = cmp[0] * kColor;
rgba[4*offset+1] = cmp[1] * kColor;
rgba[4*offset+2] = cmp[2] * kColor;
rgba[4*offset+3] = 0;
} else {
CGFloat cmp[3];
[self dc_getRGBComponents:cmp forColor:self->_innerColor];
rgba[4*offset] = cmp[0] * kColor;
rgba[4*offset+1] = cmp[1] * kColor;
rgba[4*offset+2] = cmp[2] * kColor;
rgba[4*offset+3] = 0;
}
}
offset ++;
}
}
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef bitmapContext = CGBitmapContextCreate(
rgba,
width,
height,
8, // bitsPerComponent
4*width, // bytesPerRow
colorSpace,
kCGImageAlphaNoneSkipLast);
CFRelease(colorSpace);
CGImageRef cgImage = CGBitmapContextCreateImage(bitmapContext);
free(rgba);
free(dstBuffer);
dc_getRGBComponents 这个函数主要是将UIColor转位RGBA的,具体实现可以看这篇文章:UIColor 中提取 RGBA
2、实现多边形选择区域,效果如下(我制作了gif,但是无法上传。。。):
于是我上传到资源了,效果链接为:地图编辑效果
主要用到了UIBezierPath,这里有一个技巧,在计算UIBezierPath曲线围城的区域的中心点的时候,可以调用:
_curShowText = @"Hello World";
CGRect rectx = CGPathGetBoundingBox(_path.CGPath);
[_curShowText drawAtPoint:CGPointMake(CGRectGetMidX(rectx)-10, CGRectGetMidY(rectx)) withAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:10], NSForegroundColorAttributeName:[UIColor whiteColor]}];
对,就是这个 CGPathGetBoundingBox,真的是解放了我的头脑呀!
项目demo连接:demo链接
欢迎指正,我是sven,一只小猿,如果有什么问题,请通过主页联系我。
版权声明:本文为u012094456原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。