在UIImage周围添加边框

时间:2014-07-21 15:41:51

标签: ios objective-c ios7 uiimageview uiimage

我使用下面的代码从cameraroll中为所选图像添加文本和边框。

-(UIImage*)image:(UIImage*)image withText:(NSString*)text atPoint:(CGPoint)point{
if (text) {
    CGRect rect = CGRectMake(0, 0, image.size.width, image.size.height);
    UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);
    [image drawInRect:rect];

    NSDictionary *attributes = @{NSFontAttributeName           : [UIFont boldSystemFontOfSize:80],
                                 //NSStrokeWidthAttributeName    : @(4),
                                 NSStrokeColorAttributeName    : [UIColor whiteColor]};
    [text drawAtPoint:point withAttributes:attributes];

    CALayer *l = [self.imageView layer];
    [l setBorderWidth:10.0];
    [l setBorderColor:[[UIColor blueColor] CGColor]];

    newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();    
}
return newImage;
}

当我点击我的应用程序中的保存按钮时,我将已编辑的图像保存到cameraroll。但是,在cameraroll中编辑的图像只有没有任何添加边框的文本。我是否遗漏了以上代码中的任何内容来添加和保存图像周围的边框?除了CALayer之外还有其他方法可以添加边框吗?

2 个答案:

答案 0 :(得分:1)

替换它:

CALayer *l = [self.imageView layer];
[l setBorderWidth:10.0];
[l setBorderColor:[[UIColor blueColor] CGColor]];

有了这个:

UIBezierPath *rectangleBezier = [UIBezierPath bezierPathWithRect:rect];
rectangleBezier.lineWidth = 20.0f; // 10 * 2 because line drawn from the center of edge
[[UIColor blueColor] setStroke];
[rectangleBezier stroke];

<强>说明: CALayer *l = [self.imageView layer];与UIImage数据无关。 imageView的图层更改imageView演示文稿。不是UIImage的内容。

答案 1 :(得分:0)

我将回复“在UIVmage中添加边框在UIView中

从目前为止,最简单的方法是在视图层次结构中插入另一个视图。

[self.view insertSubview:({
    UIView * border = [[UIView alloc]
        initWithFrame:CGRectInset(self.imageView.frame, -1, -1)];
    border.backgroundColor = [UIColor blueColor];

    border;
})
belowSubview:self.imageView];
相关问题