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

我想从UIView创build一个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但没有任何不同的结果。

任何build议?

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

我敢肯定,你不能像这样创buildUIImage时定义透明度 – 你可以掩盖你使用图像的UIImageView。

类似下面的function允许这种types的操作:

 // 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。

除非我错过了某些东西,否则我相信你可以通过将不透明设置为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通道。 希望这对你有用。