1

如果有人有另一个代码将这两个图像合并为一个图像,我发现下面给出了合并两个图像的代码。

当我将两个 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];
}
4

5 回答 5

6

将所有图像放在一个视图中并调用此方法,它将所有图像合并并给出新图像

+ (UIImage *) imageWithView:(UIView *)view
{

     UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0);

     [view.layer renderInContext:UIGraphicsGetCurrentContext()];

     UIImage * img = UIGraphicsGetImageFromCurrentImageContext();

     UIGraphicsEndImageContext();

     return img;

}
于 2013-04-11T12:46:49.140 回答
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;
}

我希望这个方法能解决你的问题......

于 2013-04-11T11:03:20.357 回答
1

尝试这个

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;
于 2018-06-22T06:49:24.437 回答
0

我没有给你的代码,但据我所知,你必须用它自己掩盖那个火焰图像,而不是将它与美丽的图像合并。

于 2013-04-11T12:57:54.510 回答
0

开发人员文档指出:

一张面具。如果遮罩是图像,则它必须在 DeviceGray 颜色空间中,不得具有 alpha 分量,并且本身不能被图像遮罩或遮罩颜色遮罩。如果遮罩与图像参数指定的图像大小不同,则 Quartz 缩放遮罩以适合图像。

与 Photoshop 不同,CoreGraphics 不允许通过 Alpha 通道进行遮罩,而是通过灰度进行遮罩。你确定你的面具是灰度的吗?

于 2013-04-11T09:49:22.290 回答