根据顶部图像的alpha /透明度混合两个uiimages [英] blend two uiimages based on alpha/transparency of top image
本文介绍了根据顶部图像的alpha /透明度混合两个uiimages的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试将背景与前景图像混合,其中前景图像是带有线条的透明图像。
I'm trying to blend a background with a foreground image, where the foreground image is a transparent image with lines on it.
我正在尝试这样做这样。
I am trying to do it this way.
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();
但似乎不起作用。
任何建议都将受到赞赏。
Any suggestions will be appreciated.
推荐答案
这就是我在我的应用程序中所做的,类似于Tyler的 - 但没有 UIImageView
:
This is what I've done in my app, similar to Tyler's - but without the 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
)否则你可以设置它(与 image
一样)。
If the image already has opacity, you do not need to set it (as in bottomImage
) otherwise you can set it (as with image
).
这篇关于根据顶部图像的alpha /透明度混合两个uiimages的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文