iOS使UIImage的一部分透明化 [英] iOS make part of an UIImage transparent

查看:129
本文介绍了iOS使UIImage的一部分透明化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UIImage,其中部分内容被用户选中以清除(透明)。为了做出选择,我使用了NSBezierPath。

I have an UIImage where part of it has been selected by the user to clear out (make transparent). To make the selection I used NSBezierPath.

如何在iOS中清除/制作UIImage的透明部分?

How can I clear/make transparent part of an UIImage in iOS?

推荐答案

首先,我假设您有一个UIBezierPath(iOS),而不是NSBezierPath(Mac OS X)。

First, I assume you have a UIBezierPath (iOS), not an NSBezierPath (Mac OS X).

要做这样,您将需要使用核心图形,创建图像上下文,将UIImage绘制到该上下文中,然后清除NSBezierPath指定的区域。

To do this, you will need to use core graphics, creating an image context, drawing the UIImage into that context, and then clearing the region specified by the NSBezierPath.

// Create an image context containing the original UIImage.
UIGraphicsBeginImageContext(originalImage.size);
[originalImage drawAtPoint:CGPointZero];

// Clip to the bezier path and clear that portion of the image.
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextAddPath(context,bezierPath.CGPath)
CGContextClip(context);
CGContextClearRect(context,CGRectMake(0,0,originalImage.size.width,originalImage.size.height);

// Build a new UIImage from the image context.
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

这篇关于iOS使UIImage的一部分透明化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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