简体   繁体   English

从另一个iOS中减去一个图像

[英]Subtract one image from another iOS

Anybody know how to create subtract one UIImage from another UIImage 任何人都知道如何创建从另一个UIImage中减去一个UIImage

for example as this screen: 例如,作为此屏幕:

在此输入图像描述

Thanks for response! 谢谢你的回复!

I believe you can accomplish this by using the kCGBlendModeDestinationOut blend mode. 我相信你可以通过使用kCGBlendModeDestinationOut混合模式来实现这一点。 Create a new context, draw your background image, then draw the foreground image with this blend mode. 创建新上下文,绘制背景图像,然后使用此混合模式绘制前景图像。

UIGraphicsBeginImageContextWithOptions(sourceImage.size, NO, sourceImage.scale)
[sourceImage drawAtPoint:CGPointZero];
[maskImage drawAtPoint:CGPointZero blendMode:kCGBlendModeDestinationOut alpha:1.0f];
UIImage *result = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext();

what does it mean to subtract an image? 减去图像意味着什么? the sample image given shows more of a !red operation. 给出的样本图像显示了更多的红色操作。 let us say that to subtract image a from image b means to set every pixel in b that intersects a pixel in a to transparent. 让我们说从图像中减去图像a b意味着将b中与像素相交的每个像素设置为透明。 to perform the subtraction, what we are actually doing is masking image b to the inverse of image a. 为了执行减法,我们实际做的是将图像b屏蔽到图像a的反转。 so, a good approach would be to create an image mask from the alpha channel of image a, then apply it to b. 所以,一个好的方法是从图像a的alpha通道创建一个图像蒙版,然后将其应用于b。 to create the mask you would do something like this: 创建掩码你会做这样的事情:

// get access to the image bytes
CFDataRef pixelData = CGDataProviderCopyData(CGImageGetDataProvider(image.CGImage));

// create a buffer to hold the mask values
size_t width = CGImageGetWidth(image.CGImage);
size_t height = CGImageGetHeight(image.CGImage);    
uint8_t *maskData = malloc(width * height);

// iterate over the pixel data, reading the alpha value
uint8_t *alpha = (uint8_t *)CFDataGetBytePtr(pixelData) + 3;
uint8_t *mask = maskData;
for (int y = 0; y < height; y++) {
    for (int x = 0; x < width; x++) {
        *mask = *alpha;
        mask++;      
        alpha += 4;  // skip to the next pixel
    }
}

// create the mask image from the buffer
CGDataProviderRef maskProvider = CGDataProviderCreateWithData(NULL, maskData, width * height, NULL);
CGImageRef maskImage = CGImageMaskCreate(width, height, 8, 8, width, maskProvider, NULL, false);

// cleanup
CFRelease(pixelData);
CFRelease(maskProvider);
free(maskData);

whew. 噢。 then, to mask image b, all you have to do is: 然后,要掩盖图像b,您所要做的就是:

CGImageRef subtractedImage = CGImageCreateWithMask(b.CGImage, maskImage);

hey presto. 嘿presto。

To get those results, use the second image as a mask when you draw the first image. 要获得这些结果,请在绘制第一个图像时使用第二个图像作为蒙版。 For this kind of drawing, you'll need to use Core Graphics, aka Quartz 2D. 对于这种绘图,您需要使用Core Graphics,也就是Quartz 2D。 The Quartz 2D Programming Guide has a section called Bitmap Images and Image Masks that should tell you everything you need to know. Quartz 2D Programming Guide有一个名为Bitmap Images和Image Masks的部分 ,它可以告诉你需要知道的一切。

You're asking about UIImage objects, but to use Core Graphics you'll need CGImages instead. 你问的是UIImage对象,但要使用Core Graphics,你需要使用CGImages。 That's no problem -- UIImage provides a CGImage property that lets you get the data you need easily. 这没问题CGImage提供了一个CGImage属性,可以让您轻松获取所需的数据。

Kevin Ballard's solution worked for me! Kevin Ballard的解决方案对我有用! In Swift 3.x: 在Swift 3.x中:

func subtract(source: UIImage, mask: UIImage) -> UIImage? {
    UIGraphicsBeginImageContextWithOptions(source.size, false, source.scale)
    source.draw(at: CGPoint.zero)
    mask.draw(at: CGPoint.zero, blendMode: .destinationOut, alpha: 1.0)
    let result = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return result
}

An updated answer for iOS 10+ and Swift 4+: iOS 10+和Swift 4+的更新答案:

func subtract(source: UIImage, mask: UIImage) -> UIImage? {
    return UIGraphicsImageRenderer(size: source.size).image { _ in
        source.draw(at: .zero)
        mask.draw(at: .zero, blendMode: .destinationOut, alpha: 1)
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM