【问题标题】:Transparency issue - when converting UIView to UIImage透明度问题 - 将 UIView 转换为 UIImage 时
【发布时间】:2011-02-08 13:03:00
【问题描述】:

我想从UIView 创建一个UIImage 对象。视图不是不透明的。 为此,我正在使用以下代码:

+ (UIImage *) imageWithView:(UIView *)view 
{     
    UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, [[UIScreen mainScreen] scale]);     
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];     
    UIImage * img = UIGraphicsGetImageFromCurrentImageContext();  
    UIGraphicsEndImageContext();     
    return img; 
}  

但由于某种原因,我得到的不是透明背景,而是黑色背景的图像。 我已经尝试将UIGraphicsBeginImageContextWithOptions 中的“不透明”参数设置为NO,但没有不同的结果。

有什么建议吗?

(类似问题CGContext transparency problem没有答案)

【问题讨论】:

    标签: ios uiview uiimage transparency


    【解决方案1】:

    除非我遗漏了什么,否则我相信您可以通过将 opaque 设置为 NO 来获得所需的结果

    UIGraphicsBeginImageContextWithOptions( CGSize size, BOOL opaque, CGFloat scale );
    
    
    + (UIImage *) imageWithView:(UIView *)view 
    {     
        UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, [[UIScreen mainScreen] scale]);     
        [view.layer renderInContext:UIGraphicsGetCurrentContext()];     
        UIImage * img = UIGraphicsGetImageFromCurrentImageContext();     UIGraphicsEndImageContext();     
        return img; 
    }  
    

    【讨论】:

    • 哈哈哈很好的答案。不知道为什么这没有被标记为解决方案,而是我们!
    • 不要将 opaque 参数显式设置为 NO,而是使用“view.opaque”,前提是视图已定义为不透明。这允许相同的代码适用于不透明和非透明视图。
    • 这成功了!非常感谢。不能有版主把这个设为正确答案吗?
    • 老兄,你提醒我们所有人,我们需要真正阅读函数的签名和描述......
    【解决方案2】:

    我很确定您在创建这样的 UIImage 时无法定义透明度 - 您可以屏蔽使用该图像的 UIImageView。

    类似以下的函数允许这种类型的操作:

    // handles img masking operation
    CGImageRef CopyImageAndAddAlphaChannel(CGImageRef sourceImage) {
        CGImageRef retVal = NULL;
    
        size_t width = CGImageGetWidth(sourceImage);
        size_t height = CGImageGetHeight(sourceImage);
    
        CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    
        CGContextRef offscreenContext = CGBitmapContextCreate(NULL, width, height, 
                                                              8, 0, colorSpace, kCGImageAlphaPremultipliedFirst);
    
        if (offscreenContext != NULL) {
            CGContextDrawImage(offscreenContext, CGRectMake(0, 0, width, height), sourceImage);
    
            retVal = CGBitmapContextCreateImage(offscreenContext);
            CGContextRelease(offscreenContext);
        }
    
        CGColorSpaceRelease(colorSpace);
    
        return retVal;
    }
        // png masks can mask png, gif or jpg...
    - (UIImage*)maskImage:(UIImage *)image withMask:(UIImage *)maskImage 
    {
        CGImageRef maskRef = maskImage.CGImage;
        CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(maskRef),
                                            CGImageGetHeight(maskRef),
                                            CGImageGetBitsPerComponent(maskRef),
                                            CGImageGetBitsPerPixel(maskRef),
                                            CGImageGetBytesPerRow(maskRef),
                                            CGImageGetDataProvider(maskRef), NULL, false);
    
        CGImageRef sourceImage = [image CGImage];
        CGImageRef imageWithAlpha = sourceImage;
        //add alpha channel for images that don't have one (ie GIF, JPEG, etc...)
        //this however has a computational cost
        if ((CGImageGetAlphaInfo(sourceImage) == kCGImageAlphaNone) || (CGImageGetAlphaInfo(sourceImage) == kCGImageAlphaNoneSkipFirst)) 
        { 
            imageWithAlpha = CopyImageAndAddAlphaChannel(sourceImage);
        }
    
        CGImageRef masked = CGImageCreateWithMask(imageWithAlpha, mask);
        CGImageRelease(mask);
    
        //release imageWithAlpha if it was created by CopyImageAndAddAlphaChannel
        if (sourceImage != imageWithAlpha) 
        {
            CGImageRelease(imageWithAlpha);
        }
    
        UIImage* retImage = [UIImage imageWithCGImage:masked];
        CGImageRelease(masked);
    
        return retImage;
    }
    

    你可以这样称呼它:

    UIImage *imgMasked = [self maskImage:[UIImage imageNamed:@"full_image.png"] withMask:[UIImage imageNamed:@"masked_image.png"]];
    

    masked_image.png 需要是一个 alpha 通道图像,它会切掉你的黑色背景。

    【讨论】:

    • 您能否详细说明“屏蔽我的图像视图”是什么意思?它会完成我想要达到的目标吗?
    • 可以用gif图做遮罩吗?
    • @Lemonsanver: CopyImageAndAddAlphaChannel 我把它当作未定义的符号.. 请告诉我现在可以做什么
    【解决方案3】:

    我刚刚遇到了同样的问题。对我有用的是确保我使用的是 PNG 而不是 JPG,因为 JPG 不支持 alpha 通道。希望这对你有用。

    【讨论】:

      猜你喜欢
      • 2016-11-12
      • 1970-01-01
      • 2016-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-18
      • 1970-01-01
      • 2018-01-09
      相关资源
      最近更新 更多