我怎样才能拿UIImage,并给它一个黑色的边框?

我如何设置UIImage的边框?

使用OS> 3.0,你可以这样做:

 //you need this import #import <QuartzCore/QuartzCore.h> [imageView.layer setBorderColor: [[UIColor blackColor] CGColor]]; [imageView.layer setBorderWidth: 2.0]; 

你可以通过创build一个新的图像来做到这一点(也可以在你的其他post中回答这个问题):

 - (UIImage*)imageWithBorderFromImage:(UIImage*)source; { CGSize size = [source size]; UIGraphicsBeginImageContext(size); CGRect rect = CGRectMake(0, 0, size.width, size.height); [source drawInRect:rect blendMode:kCGBlendModeNormal alpha:1.0]; CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetRGBStrokeColor(context, 1.0, 0.5, 1.0, 1.0); CGContextStrokeRect(context, rect); UIImage *testImg = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return testImg; } 

这段代码会在图像周围产生一个粉红色的边框。 但是,如果您要显示边框,则使用UIImageView的图层并设置其边框。

 #import <QuartzCore/CALayer.h> UIImageView *imageView = [UIImageView alloc]init]; imageView.layer.masksToBounds = YES; imageView.layer.borderColor = [UIColor blackColor].CGColor; imageView.layer.borderWidth = 1; 

此代码可用于添加UIImageView视图边框。

如果您知道图像的尺寸,那么在UIImageView的图层中添加边框是AFAIK的最佳解决scheme。 事实上,你可以简单地将你的imageView设置为x,y,image.size.width,image.size.height

如果你有一个固定大小的dynamic加载的图像被resize(或缩放到AspectFit)的ImageView,那么你的目标是调整图像大小调整到新的resize的图像。

最短的方法来做到这一点:

 // containerView is my UIImageView containerView.layer.borderWidth = 7; containerView.layer.borderColor = [UIColor colorWithRed:0.22 green:0.22 blue:0.22 alpha:1.0].CGColor; // this is the key command [containerView setFrame:AVMakeRectWithAspectRatioInsideRect(image.size, containerView.frame)]; 

但是要使用AVMakeRectWithAspectRatioInsideRect,则需要添加此项

 #import <AVFoundation/AVFoundation.h> 

import语句添加到您的文件中,并在您的项目中包含AVFoundation框架(与SDK捆绑在一起)。

您不能添加边框,但是这可以起到相同的效果。 你也可以在这个例子中将UIView称为blackBG到一个带有边框图像和空白中间的UIImageView中,然后你将拥有一个自定义的图像边框而不是黑色。

 UIView *blackBG = [[UIView alloc] initWithFrame:CGRectMake(0,0,100,100)]; blackBG.backgroundColor = [UIColor blackColor]; UIImageView *myPicture = [[UIImageView alloc] initWithImage: [UIImage imageNamed: @"myPicture.jpg"]]; int borderWidth = 10; myPicture.frame = CGRectMake(borderWidth, borderWidth, blackBG.frame.size.width-borderWidth*2, blackBG.frame.size.height-borderWidth*2)]; [blackBG addSubview: myPicture]; 
 imageView_ProfileImage.layer.cornerRadius =10.0f; imageView_ProfileImage.layer.borderColor = [[UIColor blackColor] CGColor]; imageView_ProfileImage.layer.borderWidth =.4f; imageView_ProfileImage.layer.masksToBounds = YES; 

您可以将边框添加到UIImageView,然后根据图像大小更改UIimageView的大小:

 #import <QuartzCore/QuartzCore.h> // adding border to the imageView [imageView.layer setBorderColor: [[UIColor whiteColor] CGColor]]; [imageView.layer setBorderWidth: 2.0]; // resize the imageView to fit the image size CGSize size = [image size]; float factor = size.width / self.frame.size.width; if (factor < size.height / self.frame.size.height) { factor = size.height / self.frame.size.height; } CGRect rect = CGRectMake(0, 0, size.width/factor, size.height/factor); imageView.frame = rect; 

确保您将imageView的原点设置为中心

所有这些答案工作正常,但添加一个图像的矩形。 假设你有一个形状(在我的例子中是一只蝴蝶),你想添加一个边框(红色边框):

我们需要两个步骤:1)把图像,转换为CGImage,传递给一个函数,使用CoreGraphics在一个上下文中画出,并给出一个新的CGImage

2)转换成画面:

 // remember to release object! + (CGImageRef)createResizedCGImage:(CGImageRef)image toWidth:(int)width andHeight:(int)height { // create context, keeping original image properties CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB(); CGContextRef context = CGBitmapContextCreate(NULL, width, height, 8 4 * width, colorspace, kCGImageAlphaPremultipliedFirst ); CGColorSpaceRelease(colorspace); if(context == NULL) return nil; // draw image to context (resizing it) CGContextSetInterpolationQuality(context, kCGInterpolationDefault); CGSize offset = CGSizeMake(2,2); CGFloat blur = 4; CGColorRef color = [UIColor redColor].CGColor; CGContextSetShadowWithColor ( context, offset, blur, color); CGContextDrawImage(context, CGRectMake(0, 0, width, height), image); // extract resulting image from context CGImageRef imgRef = CGBitmapContextCreateImage(context); CGContextRelease(context); return imgRef; 

}

 - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. CGRect frame = CGRectMake(0,0,160, 122); UIImage * img = [UIImage imageNamed:@"butterfly"]; // take low res OR high res, but frame should be the low-res one. imgV = [[UIImageView alloc]initWithFrame:frame]; [imgV setImage: img]; imgV.center = self.view.center; [self.view addSubview: imgV]; frame.size.width = frame.size.width * 1.3; frame.size.height = frame.size.height* 1.3; CGImageRef cgImage =[ViewController createResizedCGImage:[img CGImage] toWidth:frame.size.width andHeight: frame.size.height ]; imgV2 = [[UIImageView alloc]initWithFrame:frame]; [imgV2 setImage: [UIImage imageWithCGImage:cgImage] ]; // release: if (cgImage) CGImageRelease(cgImage); [self.view addSubview: imgV2]; 

}

我添加了一个正常的蝴蝶和一个红色的边界更大的蝴蝶。

你可以操作图像本身,但更好的方法是简单地添加一个包含UIImageView的UIView,并将背景改为黑色。 然后将该容器视图的大小设置为比UIImageView大一点。

另一种方法是直接从devise师那里做。

select您的图像,然后在“显示身份检查器”下。

在这里您可以手动添加“ 用户定义的运行时属性”

 layer.borderColor layer.borderWidth 

在这里输入图像说明

//你需要导入

 QuartzCore/QuartzCore.h 

然后在边框ImageView

 [imageView.layer setBorderColor: [[UIColor blackColor] CGColor]]; [imageView.layer setBorderWidth: 2.0]; [imageView.layer setCornerRadius: 5.0]; 

这个function会返回你的图像与黑色边框尝试这个..希望这会帮助你

 - (UIImage *)addBorderToImage:(UIImage *)image frameImage:(UIImage *)blackBorderImage { CGSize size = CGSizeMake(image.size.width,image.size.height); UIGraphicsBeginImageContext(size); CGPoint thumbPoint = CGPointMake(0,0); [image drawAtPoint:thumbPoint]; UIGraphicsBeginImageContext(size); CGImageRef imgRef = blackBorderImage.CGImage; CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, size.width,size.height), imgRef); UIImage *imageCopy = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); CGPoint starredPoint = CGPointMake(0, 0); [imageCopy drawAtPoint:starredPoint]; UIImage *imageC = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return imageC; } 

在Swift 3中,你是如何对UIImage本身做的:

 let size = CGSize(width: image.size.width, height: image.size.height) UIGraphicsBeginImageContext(size) let rect = CGRect(x: 0, y: 0, width: size.width, height: size.height) image?.draw(in: rect, blendMode: .normal, alpha: 1.0) let context = UIGraphicsGetCurrentContext() context?.setStrokeColor(red: 0, green: 0, blue: 0, alpha: 1) context?.stroke(rect) let newImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() self.imageView.image = newImage 

对于那些在UIImage上寻找即插即用解决scheme的人,我写了CodyMace的答案作为扩展。

用法: let outlined = UIImage(named: "something")?.outline()

 extension UIImage { func outline() -> UIImage? { let size = CGSize(width: self.size.width, height: self.size.height) UIGraphicsBeginImageContext(size) let rect = CGRect(x: 0, y: 0, width: size.width, height: size.height) self.draw(in: rect, blendMode: .normal, alpha: 1.0) let context = UIGraphicsGetCurrentContext() context?.setStrokeColor(red: 0, green: 0, blue: 0, alpha: 1) context?.stroke(rect) let newImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return newImage } } 

我使用这种方法在图像外添加边框。 您可以在boderWidth常量中自定义边框宽度。

Swift 3

 func addBorderToImage(image : UIImage) -> UIImage { let bgImage = image.cgImage let initialWidth = (bgImage?.width)! let initialHeight = (bgImage?.height)! let borderWidth = Int(Double(initialWidth) * 0.10); let width = initialWidth + borderWidth * 2 let height = initialHeight + borderWidth * 2 let data = malloc(width * height * 4) let context = CGContext(data: data, width: width, height: height, bitsPerComponent: 8, bytesPerRow: width * 4, space: (bgImage?.colorSpace)!, bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue); context?.draw(bgImage!, in: CGRect(x: CGFloat(borderWidth), y: CGFloat(borderWidth), width: CGFloat(initialWidth), height: CGFloat(initialHeight))) context?.setStrokeColor(UIColor.white.cgColor) context?.setLineWidth(CGFloat(borderWidth)) context?.move(to: CGPoint(x: 0, y: 0)) context?.addLine(to: CGPoint(x: 0, y: height)) context?.addLine(to: CGPoint(x: width, y: height)) context?.addLine(to: CGPoint(x: width, y: 0)) context?.addLine(to: CGPoint(x: 0, y: 0)) context?.strokePath() let cgImage = context?.makeImage() let uiImage = UIImage(cgImage: cgImage!) free(data) return uiImage; }