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

我想从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的“opaque”参数设置为NO但没有不同的结果。

有什么建议吗?

(类似问题CGContext透明度问题没有答案)

我很确定在创建像这样的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通道图像,它会切掉你的黑色bg。

除非我遗漏了什么,否则我相信你可以通过将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; } 

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