ios - mask 后如何去除 UIImageView 的透明区域?

标签 ios objective-c core-graphics masking cgimagemaskcreate

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

- (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')大小相同,周围有透明区域。但我想避开那些透明区域,并裁剪结果图像。我怎样才能做到这一点?面具有好几种,面具框也不尽相同。我在这里附上问题概述的引用图片。请帮助我的 friend 。提前致谢。

enter image description here

最佳答案

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

How do I autocrop a UIImage?

关于ios - mask 后如何去除 UIImageView 的透明区域?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26872568/

相关文章:

iphone - 串行写入不起作用

iphone - 当选择器 View 停止滚动时如何获取事件

ios - 有没有办法根据其父 SCNView 的框架来缩放 SCNNode?

iphone - annotationView 覆盖 drawRect,带 alpha 的图像

iPhone : How to change color of particular pixel of a UIImage?

ios - Adobe Air 应用程序在 iOS 启动画面后自动崩溃

ios - 如何在横向 View 中将 UIContainerViewCell 增加到整个页面大小

ios - 通过拖放将 CocoaLumberjack 集成到 iOS 项目中

ios - iOS 5 : UIView converted to video and the resulting video is corrupted

iOS:如何序列化不符合 NSCoding 的对象