【问题标题】:Reverse Image Masking反向图像遮罩
【发布时间】:2013-08-17 08:03:58
【问题描述】:

在 IOS SDK 中,我可以屏蔽图像但无法反转图像屏蔽。我的意思是,我有一个具有矩形部分的制作图像,现在我屏蔽图像并获得附加图像,但我想要相反的结果。

我得到图像作为结果。

虽然我需要这个作为结果。

请帮助我实现它。

...编辑... 我的代码

 UIGraphicsBeginImageContextWithOptions(self.imageView.frame.size, NO, 0.0);
CGContextRef context = UIGraphicsGetCurrentContext();

CGContextTranslateCTM(context, 0.0, self.imageView.frame.size.height);
CGContextScaleCTM(context, 1.0, -1.0);

CGImageRef maskImage = [[UIImage imageNamed:@"2.png"] CGImage];
CGContextClipToMask(context, self.imageView.bounds, maskImage);

   CGContextTranslateCTM(context, 0.0, self.imageView.frame.size.height);
   CGContextScaleCTM(context, 1.0, -1.0);

[[self.imageView image] drawInRect:self.imageView.bounds];

UIImage *image11 = UIGraphicsGetImageFromCurrentImageContext();

self.imageView.image = image11;

谢谢

【问题讨论】:

  • 你是如何屏蔽图像的?显示您的代码!
  • 请看一下。我已经编辑了我的问题并添加了我的代码..
  • 你不能只画图像然后在上面画一个白色矩形吗?
  • 谢谢,但它不是白色矩形。它是视图上的背景颜色

标签: ios uiimage masking quartz-core


【解决方案1】:

我分两步实现了它。可能不是最好的方法,但它确实有效。

  1. 反转蒙版图像。
  2. 面具

看看下面的代码。

 - (void)viewDidLoad
{
    [super viewDidLoad];

    _imageView = [[UIImageView alloc] initWithImage:i(@"test1.jpg")];
    _imageView.image = [self maskImage:i(@"face.jpg") withMask:[self negativeImage]];
    [self.view addSubview:_imageView];
}

以下方法取自here

- (UIImage *)negativeImage
{
    // get width and height as integers, since we'll be using them as
    // array subscripts, etc, and this'll save a whole lot of casting
    CGSize size = self.imageView.frame.size;
    int width = size.width;
    int height = size.height;

    // Create a suitable RGB+alpha bitmap context in BGRA colour space
    CGColorSpaceRef colourSpace = CGColorSpaceCreateDeviceRGB();
    unsigned char *memoryPool = (unsigned char *)calloc(width*height*4, 1);
    CGContextRef context = CGBitmapContextCreate(memoryPool, width, height, 8, width * 4, colourSpace, kCGBitmapByteOrder32Big | kCGImageAlphaPremultipliedLast);
    CGColorSpaceRelease(colourSpace);

    // draw the current image to the newly created context
    CGContextDrawImage(context, CGRectMake(0, 0, width, height), [self.imageView.image CGImage]);

    // run through every pixel, a scan line at a time...
    for(int y = 0; y < height; y++)
    {
        // get a pointer to the start of this scan line
        unsigned char *linePointer = &memoryPool[y * width * 4];

        // step through the pixels one by one...
        for(int x = 0; x < width; x++)
        {
            // get RGB values. We're dealing with premultiplied alpha
            // here, so we need to divide by the alpha channel (if it
            // isn't zero, of course) to get uninflected RGB. We
            // multiply by 255 to keep precision while still using
            // integers
            int r, g, b;
            if(linePointer[3])
            {
                r = linePointer[0] * 255 / linePointer[3];
                g = linePointer[1] * 255 / linePointer[3];
                b = linePointer[2] * 255 / linePointer[3];
            }
            else
                r = g = b = 0;

            // perform the colour inversion
            r = 255 - r;
            g = 255 - g;
            b = 255 - b;

            // multiply by alpha again, divide by 255 to undo the
            // scaling before, store the new values and advance
            // the pointer we're reading pixel data from
            linePointer[0] = r * linePointer[3] / 255;
            linePointer[1] = g * linePointer[3] / 255;
            linePointer[2] = b * linePointer[3] / 255;
            linePointer += 4;
        }
    }

    // get a CG image from the context, wrap that into a
    // UIImage
    CGImageRef cgImage = CGBitmapContextCreateImage(context);
    UIImage *returnImage = [UIImage imageWithCGImage:cgImage];

    // clean up
    CGImageRelease(cgImage);
    CGContextRelease(context);
    free(memoryPool);

    // and return
    return returnImage;
}

下面的方法取自here

- (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];

}

【讨论】:

  • 谢谢 Vignesh,我已经尝试过你的代码,但是用反转颜色得到了相同的结果......你有什么建议......
  • 尝试打印反转蒙版图像并检查它是否反转蒙版的 alpha。另一种方法是构建路径(使用贝塞尔曲线)并进行 EO 剪辑。如果你需要,我也可以分享我的面具图片。
  • 嗨...您能否建议任何适用于 IOS 7 的解决方案。因为它不适用于 IOS 7...。
  • 关于反向遮罩的任何想法。 (我想要填充的透明区域。),在上面的方法中,它只给出了遮罩区域....请帮助我。
  • 反转颜色与屏蔽图像无关。这怎么会成为公认的答案?
猜你喜欢
  • 2015-03-23
  • 2022-10-23
  • 2018-05-29
  • 2016-06-09
  • 2016-01-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-11
相关资源
最近更新 更多