根据顶部图像的alpha /透明度混合两个uiimages

我试图混合背景与前景图像,其中前景图像是一个透明的图像与线条。

我正在试图这样做。

UIGraphicsBeginImageContext(CGSizeMake(320, 480)); CGContextRef context = UIGraphicsGetCurrentContext(); // create rect that fills screen CGRect bounds = CGRectMake( 0,0, 320, 480); // This is my bkgnd image CGContextDrawImage(context, bounds, [UIImage imageNamed:@"bkgnd.jpg"].CGImage); CGContextSetBlendMode(context, kCGBlendModeSourceIn); // This is my image to blend in CGContextDrawImage(context, bounds, [UIImage imageNamed:@"over.png"].CGImage); UIImage *outputImage = UIGraphicsGetImageFromCurrentImageContext(); UIImageWriteToSavedPhotosAlbum(outputImage, self, nil, nil); // clean up drawing environment // UIGraphicsEndImageContext(); 

但似乎没有工作。

任何build议将不胜感激。

这是我在我的应用程序中所做的,类似于泰勒的 – 但没有UIImageView

 UIImage *bottomImage = [UIImage imageNamed:@"bottom.png"]; UIImage *image = [UIImage imageNamed:@"top.png"]; CGSize newSize = CGSizeMake(width, height); UIGraphicsBeginImageContext( newSize ); // Use existing opacity as is [bottomImage drawInRect:CGRectMake(0,0,newSize.width,newSize.height)]; // Apply supplied opacity [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height) blendMode:kCGBlendModeNormal alpha:0.8]; UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); 

如果图像已经具有不透明性,则不需要设置(如在bottomImage中),否则可以将其设置(如图像)。

 UIImage* bottomImage = [UIImage imageNamed:@"bottom.png"]; UIImage* topImage = [UIImage imageNamed:@"top.png"]; UIImageView* imageView = [[UIImageView alloc] initWithImage:bottomImage]; UIImageView* subView = [[UIImageView alloc] initWithImage:topImage]; subView.alpha = 0.5; // Customize the opacity of the top image. [imageView addSubview:subView]; UIGraphicsBeginImageContext(imageView.frame.size); [imageView.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage* blendedImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); [subView release]; [imageView release]; [self doWhateverIWantWith: blendedImage]; 

我的答案是基于Eric的回答 ,但是允许在图像合并后@ 2x图像保留其分辨率。 在我的评论中请注意URL REF,因为我承认在我的iOS应用程序中使用这个函数的开发提供了帮助。

 - (UIImage*) mergeTwoImages : (UIImage*) topImage : (UIImage*) bottomImage { // URL REF: http://iphoneincubator.com/blog/windows-views/image-processing-tricks // URL REF: https://stackoverflow.com/questions/1309757/blend-two-uiimages?answertab=active#tab-top // URL REF: http://www.waterworld.com.hk/en/blog/uigraphicsbeginimagecontext-and-retina-display int width = bottomImage.size.width; int height = bottomImage.size.height; CGSize newSize = CGSizeMake(width, height); static CGFloat scale = -1.0; if (scale<0.0) { UIScreen *screen = [UIScreen mainScreen]; if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 4.0) { scale = [screen scale]; } else { scale = 0.0; // Use the standard API } } if (scale>0.0) { UIGraphicsBeginImageContextWithOptions(newSize, NO, scale); } else { UIGraphicsBeginImageContext(newSize); } [bottomImage drawInRect:CGRectMake(0,0,newSize.width,newSize.height)]; [topImage drawInRect:CGRectMake(0,0,newSize.width,newSize.height) blendMode:kCGBlendModeNormal alpha:1.0]; UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return newImage; } 

与alpha混合

 UIGraphicsBeginImageContext(area.size); CGContextRef context = UIGraphicsGetCurrentContext(); CGContextRetain(context); // mirroring context CGContextTranslateCTM(context, 0.0, area.size.height); CGContextScaleCTM(context, 1.0, -1.0); for (...) { CGContextBeginTransparencyLayer(context, nil); CGContextSetAlpha( context, alpha ); CGContextDrawImage(context, area, tempimg.CGImage); CGContextEndTransparencyLayer(context); } // get created image UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); CGContextRelease(context); UIGraphicsEndImageContext(); 

你能提供一些细节吗?“这似乎不起作用?” 它只绘制一个图像还是其他图像? 画黑色? 噪声? 崩溃? 你为什么select了kCGBlendModeSourceIn ; 你试图达到什么效果(有几十种方法来混合图像)? 你的任何一个图像都有alpha吗?

我假设你想要做的是混合两个图像,每个有50%的不透明度? 使用CGContextSetAlpha()而不是CGContextSetBlendMode()

Swift 3

这个函数将取两个图像,一个CGSize并返回一个可选的UIImage 。 当两张图像尺寸相同时,效果最好。 如果您的顶部图像具有alpha,则会通过它显示底部图像。

 // composit two images func compositeTwoImages(top: UIImage, bottom: UIImage, newSize: CGSize) -> UIImage? { // begin context with new size UIGraphicsBeginImageContextWithOptions(newSize, false, 0.0) // draw images to context bottom.draw(in: CGRect(origin: CGPoint.zero, size: newSize)) top.draw(in: CGRect(origin: CGPoint.zero, size: newSize)) // return the new image let newImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() // returns an optional return newImage } 

用法

 let outputSize = CGSize(width: 100, height: 100) if let topImage = UIImage(named: "myTopImage") { if let bottomImage = UIImage(named: "myBottomImage") { // composite both images if let finalImage = compositeTwoImages(top: topImage, bottom: bottomImage, newSize: outputSize) { // do something with finalImage } } } 

您可以使用UIImagedrawInRect:drawAtPoint:而不是CGContextDrawImage(它们绘制到当前上下文)。 使用它们会给你带来什么不同吗?

确保从imageNamed:返回的UIImage*值也是有用的。