【问题标题】:how to change the alpha of a groundoverlay in google maps ios sdk?如何在 google maps ios sdk 中更改 groundoverlay 的 alpha?
【发布时间】:2013-05-15 08:16:09
【问题描述】:

我在地图视图中添加了一个groundoverlay,我找到了改变groundoverlay.icon alpha 的方法。 How to set the opacity/alpha of a UIImage? 但它似乎对应用程序没有影响,我仍然看不到图像后面的地图或其他地面覆盖物。 有解决办法吗?

【问题讨论】:

    标签: ios objective-c google-maps uiimage alpha


    【解决方案1】:
    + (UIImage *) setImage:(UIImage *)image withAlpha:(CGFloat)alpha
    

    {

    // Create a pixel buffer in an easy to use format
    CGImageRef imageRef = [image CGImage];
    NSUInteger width = CGImageGetWidth(imageRef);
    NSUInteger height = CGImageGetHeight(imageRef);
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    
    UInt8 * m_PixelBuf = malloc(sizeof(UInt8) * height * width * 4);
    
    NSUInteger bytesPerPixel = 4;
    NSUInteger bytesPerRow = bytesPerPixel * width;
    NSUInteger bitsPerComponent = 8;
    CGContextRef context = CGBitmapContextCreate(m_PixelBuf, width, height,
                                                 bitsPerComponent, bytesPerRow, colorSpace,
                                                 kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
    
    CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
    CGContextRelease(context);
    
    //alter the alpha
    int length = height * width * 4;
    for (int i=0; i<length; i+=4)
    {
        m_PixelBuf[i+3] =  255*alpha;
    }
    
    
    //create a new image
    CGContextRef ctx = CGBitmapContextCreate(m_PixelBuf, width, height,
                                             bitsPerComponent, bytesPerRow, colorSpace,
                                             kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
    
    CGImageRef newImgRef = CGBitmapContextCreateImage(ctx);
    CGColorSpaceRelease(colorSpace);
    CGContextRelease(ctx);
    free(m_PixelBuf);
    
    UIImage *finalImage = [UIImage imageWithCGImage:newImgRef];
    CGImageRelease(newImgRef);
    
    return finalImage;
    

    }

    【讨论】:

    • 自己寻找解决方案。似乎有一个可通过 JavaScript API 设置的不透明度选项(请参阅stackoverflow.com/questions/11132537/…),但我还没有弄清楚如何通过 iOS API 访问它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多