Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 透明度问题-将UIView转换为UIImage时_Ios_Uiview_Uiimage_Transparency - Fatal编程技术网

Ios 透明度问题-将UIView转换为UIImage时

Ios 透明度问题-将UIView转换为UIImage时,ios,uiview,uiimage,transparency,Ios,Uiview,Uiimage,Transparency,我想从UIView创建一个UIImage对象。视图不是不透明的。 为此,我使用以下代码: + (UIImage *) imageWithView:(UIView *)view { UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, [[UIScreen mainScreen] scale]); [view.layer renderInContext:UIGraphicsG

我想从
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
,但没有不同的结果

有什么建议吗


(类似的问题没有答案)

我敢肯定,在创建这样的UIImages时,您不能定义透明度-您可以屏蔽使用该图像的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通道图像,它将切掉你的黑色背景。

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

除非我遗漏了什么,否则我相信您可以通过将“不透明”设置为“否”来获得想要的结果

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; 
}  

你能告诉我更多的细节吗?你说的“掩蔽我的图像视图”是什么意思?它能完成我想要达到的目标吗?gif图像能用作掩蔽图像吗?@Lemonsanver:CopyImageandDadalphaChannel我把它作为未定义的符号。。请告诉我现在能做什么哈哈哈回答得好。不确定为什么这没有标记为解决方案,但w.e!如果视图已定义为非不透明,请改用“view.opaque”,而不是显式将opaque参数设置为NO。这允许相同的代码用于不透明和非不透明视图。这很有效!非常感谢。难道一些版主不能将此设置为正确答案吗?伙计,你提醒了我们所有人,我们需要实际阅读函数的签名和对它们的描述。。。