iOS- 带有alpha的UIImage在iOS13的MKOverlayRenderer中具有褪色的颜色

问题描述 投票:0回答:1

我具有从PNG加载的RGBA MKOverlayRendererUIImage。该图像的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。但是,当我在地图上渲染图像时,...

objective-c uiimage mkmapview alpha
1个回答
0
投票

遇到同一问题:

© www.soinside.com 2019 - 2024. All rights reserved.