简体   繁体   English

iOS SDK-图像遮罩

[英]iOS SDK - Image masking

How to mask an image with another image by non transparent pixels? 如何用非透明像素用另一个图像遮盖一个图像? Eg 例如

在此处输入图片说明

When the mask image is black&white, I use this function: 当蒙版图像为黑白时,我使用以下功能:

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

But how to do this in my case? 但是在我的情况下该如何做呢?

Here are the steps, to achieve the result 这是实现结果的步骤

1) Create an image view, and calculate the optimal size for it (+ set aspectFit option) 1)创建一个图像视图,并为其计算最佳尺寸(+设置AspectFit选项)

2) Get the image view frame, and create a mask with that options (ie scale your predefined mask image) 2)获取图像视图框架,并使用该选项创建遮罩(即缩放您的预定义遮罩图像)

3) Mask image view, with your created mask 3)使用创建的蒙版蒙版图像视图

UIImageView *maskView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"star_mask_alpha.png"]];
    self.needsMaskImageView.layer.mask = maskView.layer;
    [self.needsMaskImageView setNeedsDisplay];

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

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