iphone - 在定义的路径内裁剪 UIImage

标签 iphone ios objective-c ipad

<分区>

Possible Duplicate:
drawing UIImage inside a CGMutablePath

我的 drawRect 中有一条路径,如下所示:

CGContextSetLineWidth(context, 1.5);
CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor);    
CGContextAddEllipseInRect(context, rect);
CGContextStrokePath(context);

CGContextClip(context);
CGContextTranslateCTM(context, 0.0, rect.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextDrawImage(context, rect, self.image_.CGImage);

UIGraphicsBeginImageContext(rect.size);
UIImage * circleUserProfile = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

[circleUserProfile drawAtPoint:CGPointMake(0, 0)];

CGContextRestoreGState(context);

我有一个 UIImage,我想让 UIImage 夹在这个圆圈内。上面的代码似乎在做我之前问过的here ,但仍然不起作用。这是为什么?

最佳答案

当您调用 UIGraphicsBeginImageContext 时,您正在创建一个新的上下文,推送您之前的上下文(您在其中完成所有绘图),并使新的(空的)上下文成为当前上下文。在调用 UIGraphicsGetCurrentContext 并执行绘图之前,您需要开始图像上下文。

关于iphone - 在定义的路径内裁剪 UIImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14367954/

相关文章:

iphone - 如何在 sqlite 中创建时间戳字段,以便正确初始化具有 NSDate 字段的核心数据对象?

objective-c - pushViewController:detail 不推送 detailView

ios - 核心数据缓慢获取关系对象

ios - 如何从具有分页功能的 API 加载 UITableView 的数据?

iphone - 将非 ARC 项目转换为 ARC 错误

iphone - 调整多个 UILabel 的位置

javascript - 检测 UIWebView 是否可以在 iPhone 上内联播放视频 ( allowsInlineMediaPlayback )

ios - 将枚举与 Swift 中的关联值进行比较

ios - iPhone 上的 ComScore 分析

ios - 获取 iOS 今天扩展的宽度