我的问题对你来说可能很容易.
我想在我的照片编辑应用程序中为UIImage添加白色边框.我找到了一些问题和答案How can i take an UIImage and give it a black border?.
大多数答案都是为UIImageView添加边框.在我的情况下,我需要为UIImage添加一个边框宽度可调的边框.
请问你能帮帮我吗?
@R_403_323@
您可以通过访问UIImageView的图层属性来设置CALayer上的边框属性.
首先,添加Quartz
#import <QuartzCore/QuartzCore.h>
设置属性:
[[yourImageView layer] setBorderWidth:2.0f]; [[yourImageView layer] setBorderColor:[UIColor whiteColor].CGColor];
编辑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,(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,height); CGContextAddLineToPoint(ctx,0.0); CGContextAddLineToPoint(ctx,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; }