我具有从PNG加载的RGBA MKOverlayRenderer
的UIImage
。该图像的alpha值对于透明部分为0,对于半透明部分为160。
但是,当我在地图上绘制图像时,颜色被洗掉了,整个图像看起来像是在雾中。在更新到iOS 13之后,就会发生此问题,旧版本可以按预期工作。
我通过[UIImage imageNamed:@"some_img"];
加载图像
我有扩展MKOverlayRenderer
,并覆盖了drawMapRect
方法:
- (void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)context {
UIImage * img = [UIImage imageNamed:@"some_img"];
MKMapRect theMapRect = self.overlay.boundingMapRect;
CGRect theRect = [self rectForMapRect:theMapRect];
CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextTranslateCTM(context, 0.0, -theRect.size.height);
CGContextDrawImage(context, theRect, img.CGImage);
}
我已经尝试通过创建图像的副本并使用此公式手动处理RGBA像素来从图像中手动删除预乘的alpha(将alpha设置为255而不是160,设置为255):
CGImageRef sourceImage = img.CGImage; CFDataRef theData; theData = CGDataProviderCopyData(CGImageGetDataProvider(sourceImage)); UInt8 *pixelData = (UInt8 *) CFDataGetBytePtr(theData); int dataLength = CFDataGetLength(theData); for (int i = 0; i < dataLength; i += 4){ uint8_t aHalf = pixelData[i + 3] / 2; pixelData[i + 0] = (pixelData[i + 0] * 255 + aHalf) / pixelData[i + 3]; pixelData[i + 1] = (pixelData[i + 1] * 255 + aHalf) / pixelData[i + 3]; pixelData[i + 2] = (pixelData[i + 2] * 255 + aHalf) / pixelData[i + 3]; pixelData[i + 3] = (pixelData[i + 3] == 0) ? 0 : 255; } CGContextRef context; context = CGBitmapContextCreate(pixelData, CGImageGetWidth(sourceImage), CGImageGetHeight(sourceImage), 8, CGImageGetBytesPerRow(sourceImage), CGImageGetColorSpace(sourceImage), kCGImageAlphaPremultipliedLast); UIImage *newImage = [UIImage imageWithCGImage:CGBitmapContextCreateImage(context)];
然后我将
CGContextSetAlpha(context, 0.6);
设置为drawMapRect
。
这部分解决了问题,但是在较旧的iOS版本上,图像的透明性不如在iOS 13上透明,并且整个过程需要时间。
我有从PNG加载RGBA UIImage的MKOverlayRenderer。图像的alpha值对于透明部分为0,对于半透明部分为160。但是,当我在地图上渲染图像时,...
遇到同一问题: