在iOS中将两个uiimageview合并为单个uiimageview

kag*_*noj 1 iphone objective-c ipad xcode4.5

我发现下面的代码是合并两个图像,如果有任何人有另一个代码将这两个图像合并为一个图像.

当我将两个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];
}
Run Code Online (Sandbox Code Playgroud)

yen*_*yen 6

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

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

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

     [view.layer renderInContext:UIGraphicsGetCurrentContext()];

     UIImage * img = UIGraphicsGetImageFromCurrentImageContext();

     UIGraphicsEndImageContext();

     return img;

}
Run Code Online (Sandbox Code Playgroud)