遮罩后如何删除UIImageView的透明区域? [英] How to remove the transparent area of an UIImageView after masking?

查看:120
本文介绍了遮罩后如何删除UIImageView的透明区域?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的一个iOS应用程序中,我试图使用 CGImageMask 剪切图像的一部分。我已经使用以下代码成功屏蔽了图像:

In one of my iOS applications, I am trying to cut a portion of an image using 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];
}

因此,我的图像将是:

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

问题:
输出图像的大小相同周围具有透明区域的参考图像('image.png')。但是我想避免这些透明区域,并裁剪结果图像。我该如何实现?有几个遮罩,并且遮罩框架并非全部相似。我在这里附上问题概述的参考图像。请帮我的朋友们。

Problem: The output image is of the same size of reference image('image.png') with transparent area around. 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.

推荐答案

查找自动裁剪的UIImage。这应该裁剪出任何透明的东西。

Look up auto-cropping a UIImage. This should crop out anything transparent.

如何自动裁剪UIImage?

这篇关于遮罩后如何删除UIImageView的透明区域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆