首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iOS UIImageView剪辑/蒙版图像

iOS UIImageView剪辑/蒙版图像
EN

Stack Overflow用户
提问于 2012-12-26 04:57:22
回答 1查看 1.6K关注 0票数 0

基本上,我正在制作一个包含两个图像的视图。图一显示在视图左上角的右三角形中,图二显示右上三角形,右下角。

想象一个对角线切割的正方形,每一半都有不同的图像。

我读了很多关于面具的文章,但我不想用另一张图片来遮罩这些图片。

我正在寻找一种方法来给它3个点,形成那个三角形,然后让它以这种方式裁剪图像。

我觉得这在Coregraphics中可能很容易做到,我只是错过了我认为的调用。

任何帮助都是非常感谢的!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-12-26 06:18:16

这里有一种方法,在图像上下文中使用裁剪路径。该示例是针对256x256的图像大小的,但是您应该能够很容易地使其适应您的需要。

代码语言:javascript
复制
UIGraphicsBeginImageContext(CGSizeMake(256, 256));
CGContextRef context = UIGraphicsGetCurrentContext();
CGAffineTransform flipVertical = CGAffineTransformMake(1, 0, 0, -1, 0, 256); 
CGContextConcatCTM(context, flipVertical);
CGContextBeginPath(context);
CGContextMoveToPoint(context, 0, 0);
CGContextAddLineToPoint(context, 0, 256);
CGContextAddLineToPoint(context, 256, 256);
CGContextClosePath(context);
CGContextSaveGState(context);
CGContextClip(context);
CGContextDrawImage(context, CGRectMake(0, 0, 256, 256), [image1 CGImage]);
CGContextRestoreGState(context);
CGContextBeginPath(context);
CGContextMoveToPoint(context, 0, 0);
CGContextAddLineToPoint(context, 256, 0);
CGContextAddLineToPoint(context, 256, 256);
CGContextClosePath(context);
CGContextSaveGState(context);
CGContextClip(context);
CGContextDrawImage(context, CGRectMake(0, 0, 256, 256), [image2 CGImage]);
CGContextRestoreGState(context);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); // image contains the result
UIGraphicsEndImageContext();
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14033945

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档