我发现下面给出了合并两个图像的代码,如果有人有另一个代码将这两个图像合并为一个图像。
当我将两个uiimageview合并到一个uiimageview中时,最终图像变为黑色或白色阴影。实际遮蔽图像的颜色不会出现在最终图像中
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.navigationController.navigationBarHidden = YES;
imgBack = [UIImage imageNamed:@"img1.jpg"];
imgMask = [UIImage imageNamed:@"img2.png"];
imgBackground.image = imgBack;
imgMasking.image = imgMask;
imgFinally = [self maskImage:imgBack withMask:imgMask];
imgFinal.image = imgFinally;
imgBackground.image= nil;
imgMasking.image = nil;
}
- (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 masked = CGImageCreateWithMask([image CGImage], mask);
return [UIImage imageWithCGImage:masked];
}
答案 0 :(得分:6)
将所有图像放在一个视图中并调用此方法,它会合并所有图像并提供新图像
+ (UIImage *) imageWithView:(UIView *)view
{
UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
答案 1 :(得分:2)
嗨试试这个......
UIImage* bottomImage = [UIImage imageNamed:@"index.jpg"];
UIImage* topImage = [UIImage imageNamed:@"tiger.png"];
UIImageView* imageView = [[UIImageView alloc] initWithImage:bottomImage];
UIImageView* subView = [[UIImageView alloc] initWithImage:topImage];
[imageView addSubview:subView];
UIGraphicsBeginImageContext(imageView.frame.size);
CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeMultiply);
[imageView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage* blendedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[subView release];
[imageView release];
self.resultImage.image=blendedImage; //Your Image set
针对您的问题的最终修改方法: -
- (UIImage*) mergeTwoImages : (UIImage*) topImage : (UIImage*) bottomImage
{
// URL REF: http://iphoneincubator.com/blog/windows-views/image-processing-tricks
// URL REF: http://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:kCGBlendModeLighten alpha:1.0];
[topImage drawInRect:CGRectMake(0,0,newSize.width,newSize.height) blendMode:kCGBlendModeMultiply alpha:1.0];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
我希望这种方法可以解决你的问题......
答案 2 :(得分:0)
开发者文档声明:
面具。如果蒙版是图像,则它必须是DeviceGray颜色 空间,不得有alpha分量,也可能不会被掩盖 通过图像掩模或掩蔽颜色。如果面具尺寸不同 作为图像参数指定的图像,然后Quartz缩放 掩模以适合图像。
与...不同Photoshop,CoreGraphics不允许通过alpha通道屏蔽,而是通过灰度级屏蔽。你确定你的面具是灰度的吗?
答案 3 :(得分:0)
我没有你的代码,但据我所知,你必须用它自己掩盖火焰图像,然后将它合并到具有美感的图像中。
答案 4 :(得分:0)
尝试一下
UIImage *image1 = [UIImage imageNamed:@"image1.png"];
UIImage *image2 = [UIImage imageNamed:@"image2.png"];
CGSize size = CGSizeMake(image1.size.width, image1.size.height);
UIGraphicsBeginImageContext(size);
[image1 drawInRect:CGRectMake(0,0,size.width, image1.size.height)];
[image2 drawInRect:CGRectMake(0,0,size.width, image2.size.height)];
UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//Add image to view
imageView.image = finalImage;