如果有人有另一个代码将这两个图像合并为一个图像,我发现下面给出了合并两个图像的代码。
当我将两个 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];
}