简体   繁体   English

如何在屏蔽后删除UIImageView的透明区域?

[英]How to remove the transparent area of an UIImageView after masking?

In one of my iOS applications, I am trying to cut a portion of an image using CGImageMask . 在我的一个iOS应用程序中,我试图使用CGImageMask剪切图像的CGImageMask I have succeeded in masking the image with the following code: 我已成功使用以下代码屏蔽图像:

- (UIImage *)maskImage:(UIImage *)referenceImage 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([referenceImage CGImage], mask);
    return [UIImage imageWithCGImage:masked];
}

So, my image will be: 所以,我的形象将是:

myImageView.image = [self maskImage:[UIImage imageNamed:@"image.png"] 
                           withMask:[UIImage imageNamed:@"mask.png"]];

Problem: The output image is of the same size of reference image('image.png') with transparent area around. 问题:输出图像与参考图像('image.png')的大小相同,周围有透明区域。 But I want to avoid those transparent area, and crop the result image. 但我想避开那些透明区域,并裁剪结果图像。 How can I achieve this? 我怎样才能做到这一点? There are several masks, and the mask frames are not similar to all. 有几个掩模,并且掩模框架与所有掩模框架不相似。 I am attaching a reference image of the problem overview here. 我在这里附上问题概述的参考图像。 Please help me friends. 请帮帮我的朋友。 Thanks in advance. 提前致谢。

在此输入图像描述

Look up auto-cropping a UIImage. 查找自动裁剪UIImage。 This should crop out anything transparent. 这应该是透明的。

How do I autocrop a UIImage? 如何自动切换UIImage?

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

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