为UIImage添加白色边框

sas*_*sha 6 objective-c uiimage ios

我的问题对你来说可能很容易.

我想UIImage在我的照片编辑应用程序中添加白色边框.我发现了一些问题和答案我如何拍摄UIImage并给它一个黑色边框?.

大多数答案都是为了添加边框UIImageView.在我的情况下,我需要添加一个UIImage可调节边框宽度的边框.

请问你能帮帮我吗?

Fah*_*kar 12

您可以通过访问UIImageView的图层属性来设置CALayer上的边框属性.

首先,添加Quartz

#import <QuartzCore/QuartzCore.h>
Run Code Online (Sandbox Code Playgroud)

设置属性:

[[yourImageView layer] setBorderWidth:2.0f];
[[yourImageView layer] setBorderColor:[UIColor whiteColor].CGColor];
Run Code Online (Sandbox Code Playgroud)

编辑1

由于需要使用边框保存图像,请使用以下内容.

- (UIImage *)addBorderToImage:(UIImage *)image {
    CGImageRef bgimage = [image CGImage];
    float width = CGImageGetWidth(bgimage);
    float height = CGImageGetHeight(bgimage);

        // Create a temporary texture data buffer
    void *data = malloc(width * height * 4);

    // Draw image to buffer
    CGContextRef ctx = CGBitmapContextCreate(data,
                                                 width,
                                                 height,
                                                 8,
                                                 width * 4,
                                                 CGImageGetColorSpace(image.CGImage),
                                                 kCGImageAlphaPremultipliedLast);
    CGContextDrawImage(ctx, CGRectMake(0, 0, (CGFloat)width, (CGFloat)height), bgimage);

    //Set the stroke (pen) color
    CGContextSetStrokeColorWithColor(ctx, [UIColor greenColor].CGColor);

    //Set the width of the pen mark
    CGFloat borderWidth = (float)width*0.05;
    CGContextSetLineWidth(ctx, borderWidth);

    //Start at 0,0 and draw a square
    CGContextMoveToPoint(ctx, 0.0, 0.0);    
    CGContextAddLineToPoint(ctx, 0.0, height);
    CGContextAddLineToPoint(ctx, width, height);
    CGContextAddLineToPoint(ctx, width, 0.0);
    CGContextAddLineToPoint(ctx, 0.0, 0.0);

    //Draw it
    CGContextStrokePath(ctx);

        // write it to a new image
    CGImageRef cgimage = CGBitmapContextCreateImage(ctx);
    UIImage *newImage = [UIImage imageWithCGImage:cgimage];
    CFRelease(cgimage);
    CGContextRelease(ctx);

        // auto-released
    return newImage;
}
Run Code Online (Sandbox Code Playgroud)

参考


Ric*_*ich 5

您需要创建一个新的上下文,然后将图像重新绘制为:

UIImage *image = yourImage;
UIGraphicsBeginImageContextWithOptions(image.size, NO, image.scale);
[image drawAtPoint:CGPointZero];
[[UIColor whiteColor] setStroke];
UIRectFrame(CGRectMake(0, 0, image.size.width, image.size.height));
UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Run Code Online (Sandbox Code Playgroud)

UIRectFrame绘制 1 点边框。如果您需要更多,则需要使用 aUIBezierPath代替。

UIImage *image = yourImage;
UIGraphicsBeginImageContextWithOptions(image.size, NO, image.scale);
[image drawAtPoint:CGPointZero];
[[UIColor whiteColor] setStroke];
UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, image.size.width, image.size.height)];
path.lineWidth = 2.0;
[path stroke];
UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Run Code Online (Sandbox Code Playgroud)

对于作为类别的完整实现UIImage

UIImage+Bordered.h

@interface UIImage (Bordered)

-(UIImage *)imageBorderedWithColor:(UIColor *)color borderWidth:(CGFloat)width;

@end
Run Code Online (Sandbox Code Playgroud)

UIImage+Bordered.m

@implementation UIImage (Bordered)

-(UIImage *)imageBorderedWithColor:(UIColor *)color borderWidth:(CGFloat)width
{
    UIGraphicsBeginImageContextWithOptions(self.size, NO, self.scale);
    [self drawAtPoint:CGPointZero];
    [color setStroke];
    UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, self.size.width, self.size.height)];
    path.lineWidth = width;
    [path stroke];
    UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return result;
}

@end
Run Code Online (Sandbox Code Playgroud)

然后使用它就像这样简单:

UIImage *image = yourImage;
UIImage *borderedImage = [image imageBorderedWithColor:[UIColor whiteColor] borderWidth:2.0];
Run Code Online (Sandbox Code Playgroud)