【问题标题】:Easiest Way to Put a Circular Frame Around a UIImage在 UIImage 周围放置圆形框架的最简单方法
【发布时间】:2011-08-25 21:14:47
【问题描述】:

我正在尝试为我正在构建的其中一个应用程序制作一个很酷的效果,并且宁愿不使用 drawRect 函数。你们能想出一个简单的方法在 UIImage 周围放置一个漂亮的圆形边框吗?它应该看起来像一个圆形相框。

这是我目前使用的:

CGFloat radius = self.layer.bounds.size.width / 2;

    CAShapeLayer *mask = [CAShapeLayer layer];
    mask.frame = self.layer.bounds;
    [mask setFillColor:[[UIColor whiteColor] CGColor]];

    CGFloat width = self.layer.bounds.size.width;
    CGFloat height = self.layer.bounds.size.height;
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, NULL, width, 0);
    CGPathAddLineToPoint(path, NULL, width, height - radius);
    CGPathAddCurveToPoint(path, NULL, width, height, width - radius, height, width - radius, height);
    CGPathAddLineToPoint(path, NULL, 0, height);
    CGPathAddLineToPoint(path, NULL, 0, radius);
    CGPathAddCurveToPoint(path, NULL, 0, 0, radius, 0, radius, 0);
    CGPathCloseSubpath(path);
    [mask setPath:path];
    CGPathRelease(path);

    self.layer.mask = mask; 

    [self setNeedsDisplay];

我的 UIImage 被封装在一个 UIView 中(self 是一个 UIView)。我期待它在我的 UIView 周围画一个圆圈。

【问题讨论】:

  • 您是在问如何在不使用视图的情况下绘制图像,还是在问是否可以在不实际绘制的情况下绘制图像?
  • 我正在尝试在我的图像周围画一个圆圈

标签: iphone objective-c ios


【解决方案1】:

可能最好的方法是使用 QuartzCore。

基本上,您需要包含 QuartzCore 框架,然后创建一个 UIView 来放入您的图像,然后将其圆角并将其 clipsToBounds 属性设置为 YES。

例子:

#include <QuartzCore/QuartzCore.h>

//More code stuff

UIView *newView = [[UIView alloc] initWithFrame:frame];
newView.clipsToBounds = YES;
newView.layer.cornerRadius = 10;
[newView addSubview:imageView];

【讨论】:

  • 我发现这种方法的唯一问题是,如果您想在图像上添加阴影,您将遇到麻烦,因为您必须使用 clipToBounds。
  • 您遇到麻烦是绝对正确的。这就是为什么我在评论中说,“...创建一个 UIView 将您的图像放入...”,在某些情况下这不是一个完美的答案,但它确实可以让您获得所需的效果。
【解决方案2】:

您可以在 UIImageView 的图层上使用遮罩,或者将角半径设置为高度的一半。无论哪种情况,请务必#import &lt;QuartzCore/QuartzCore.h&gt;

myImageView.layer.cornerRadius = myImageView.bounds.size.width / 2;

制作两个圆角的示例蒙版(改为使其成为圆形):

+ (void) applyTwoCornerMask: (CALayer *) layer withRadius: (CGFloat) radius {
    CAShapeLayer *mask = [CAShapeLayer layer];
    mask.frame = layer.bounds;
    [mask setFillColor:[[UIColor blackColor] CGColor]];

    CGFloat width = layer.bounds.size.width;
    CGFloat height = layer.bounds.size.height;
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, NULL, width, 0);
    CGPathAddLineToPoint(path, NULL, width, height - radius);
    CGPathAddCurveToPoint(path, NULL, width, height, width - radius, height, width - radius, height);
    CGPathAddLineToPoint(path, NULL, 0, height);
    CGPathAddLineToPoint(path, NULL, 0, radius);
    CGPathAddCurveToPoint(path, NULL, 0, 0, radius, 0, radius, 0);
    CGPathCloseSubpath(path);
    [mask setPath:path];
    CGPathRelease(path);

    layer.mask = mask;  
}

【讨论】:

  • 由于某种原因,这对我不起作用。这是代码:代码不适合评论,请检查编辑
【解决方案3】:

以下方法如何: 使用位于原始顶部的第二个 UIImageView,其中包含您想要构图的图像。这第二个 UIImageView 具有您的相框图像的可拉伸版本,因此如果原始图像改变大小,您的相框可以改变。

不容易重复使用,但会很简单。

您将使用 UIImage 的 stretchableImageWithLeftCapWidth 方法来创建相框图像的可拉伸版本。

【讨论】:

    猜你喜欢
    • 2010-10-09
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-28
    • 2021-01-13
    相关资源
    最近更新 更多