0

我想在 iOS 中的两个图像之间进行选择性遮罩,类似于 Blender 中的遮罩功能。有两个图像 1 和 2(调整为相同的尺寸)。最初只有图像 1 是可见的,但是无论用户在哪里触摸 image1 上的任何区域,它都会变得透明,并且图像 2 在这些区域中变得可见。

我使用带有触摸移动的核心图形创建了一个类似蒙版的图像。它基本上是一个全黑的图像,在我触摸的任何地方都有白色部分。alpha 始终设置为 1.0。我可以使用这个图像作为掩码,并通过实现我自己的图像处理方法来做必要的事情,这些方法将遍历每个像素,检查它并设置相应的值。现在这个方法将在 touch move 内部调用,所以我的方法可能会减慢整个过程(特别是对于 8MP 相机图像)。

我想知道如何通过使用 Quartz Core 或 Core Graphics 来实现这一点,它们的效率足以在大图像中运行。

我到目前为止的代码:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    mouseSwiped = YES;

    UITouch *touch = [touches anyObject];
    CGPoint currentPoint = [touch locationInView:staticBG];

    UIGraphicsBeginImageContext(staticBG.frame.size);
    [maskView.image drawInRect:CGRectMake(0, 0, maskView.frame.size.width, maskView.frame.size.height)];

    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 20.0);
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 1.0, 1.0, 0.0);
    CGContextBeginPath(UIGraphicsGetCurrentContext());
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
    CGContextStrokePath(UIGraphicsGetCurrentContext());

    maskView.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    lastPoint = currentPoint;
    mouseMoved++;
    if (mouseMoved == 10)
        mouseMoved = 0;

    staticBG.image = [self maskImage:staticBG.image withMask:maskView.image];
    //maskView.hidden = NO;
}  

- (UIImage*) maskImage:(UIImage *)baseImage withMask:(UIImage *)maskImage
{
    CGImageRef imgRef = [baseImage CGImage];
    CGImageRef maskRef = [maskImage CGImage];
    CGImageRef actualMask = CGImageMaskCreate(CGImageGetWidth(maskRef),
                                          CGImageGetHeight(maskRef),
                                          CGImageGetBitsPerComponent(maskRef),
                                          CGImageGetBitsPerPixel(maskRef),
                                          CGImageGetBytesPerRow(maskRef),
                                          CGImageGetDataProvider(maskRef), NULL, false);
    CGImageRef masked = CGImageCreateWithMask(imgRef, actualMask);
    return [UIImage imageWithCGImage:masked];
}

maskImage 方法不起作用,因为它根据 alpha 值创建掩码图像。我浏览了这个链接:从路径创建蒙版,但我无法理解答案。

4

3 回答 3

0

由于您是实时进行的,因此建议您在编辑时进行伪造,如果以后需要输出图像,可以将其蒙版,因为可能需要一些时间(不多,只是不够快实时进行)。通过伪装,我的意思是将 image1 作为背景,并在其上放置隐藏的 image2。一旦用户触摸该点,将 image2 UIImageView 的边界设置为 CGRect rect= CGRectMake(touch.x - desiredRect.size.width/2, touch.y - desiredRect.size.height/2, desiredRect.size.width, desiredRect.size.height); 并使其可见。 desiredRect将是您要显示的 image2 的一部分。抬起手指后,您可以隐藏 image2 UIImageView,以便 image1 完全可见。如果您的目标不是在那一刻输出图像,这是我现在能想到的最快方法。

于 2013-10-05T10:38:51.173 回答
0

使用此代码将有助于屏蔽两个 UIImage

CGSize newSize = CGSizeMake(320, 377);
                UIGraphicsBeginImageContext( newSize );

            // Use existing opacity as is
            [ backGroundImageView.image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
            // Apply supplied opacity
            [self.drawImage.image drawInRect:CGRectMake(0,0,newSize.width,newSize.height) blendMode:kCGBlendModeNormal alpha:0.8];

            UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();

            UIGraphicsEndImageContext();

            imageData = UIImagePNGRepresentation(newImage);
于 2013-10-05T10:48:28.427 回答
0

首先,我会提到一些我希望你已经知道的事情。

  • 屏蔽仅通过获取 alpha 值起作用。
  • 在每次 touchMove 时创建一个带有核心图形的图像是一个相当大的开销,您应该尽量避免或使用其他一些做事方式。
  • 尝试使用静态蒙版图像。

我想建议你从一个倒置的角度来看待这个问题。

即,与其尝试在顶部图像中打孔以查看底部,不如将底部图像放在顶部并遮盖底部图像,以便显示用户的触摸点覆盖特定部分的顶部视图。?

我已经做了一个例子让你在这里得到一个想法> http://goo.gl/Zlu31T

祝你好运,如果有任何不清楚的地方,请回复。我确实相信有更好和优化的方法来做到这一点。“_”

于 2013-10-05T09:47:14.607 回答