ios 我怎样才能获取一个 UIImage 并给它一个黑色边框?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1354811/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 16:22:01  来源:igfitidea点击:

How can i take an UIImage and give it a black border?

iphoneiosobjective-cuiimageviewuiimage

提问by Shay

How can I set the border of a UIImage?

如何设置 a 的边框UIImage

回答by mclin

With OS > 3.0 you can do this:

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

//you need this import
#import <QuartzCore/QuartzCore.h>

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

回答by Marcus S. Zarra

You can do this by creating a new image (also answered in your other posting of this question):

您可以通过创建一个新图像来做到这一点(也在您的其他帖子中回答了这个问题):

- (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;
}  

This code will produce a pink border around the image. However if you are going to just display the border then use the layer of the UIImageViewand set its border.

此代码将在图像周围产生粉红色边框。但是,如果您只想显示边框,请使用 的图层UIImageView并设置其边框。

回答by Faizan Refai

#import <QuartzCore/CALayer.h>


UIImageView *imageView = [UIImageView alloc]init];
imageView.layer.masksToBounds = YES;
imageView.layer.borderColor = [UIColor blackColor].CGColor;
imageView.layer.borderWidth = 1;    

This code can be used for adding UIImageViewview border.

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

回答by Bijendra Singh

imageView_ProfileImage.layer.cornerRadius =10.0f;
imageView_ProfileImage.layer.borderColor = [[UIColor blackColor] CGColor];
imageView_ProfileImage.layer.borderWidth =.4f;
imageView_ProfileImage.layer.masksToBounds = YES;

回答by ScorpionKing2k5

If you know the dimensions of your image, then adding a border to the UIImageView's layer is the best solution AFAIK. Infact, you can simply setFrame your imageView to x,y,image.size.width,image.size.height

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

In case you have an ImageView of a fixed size with dynamically loaded images which are getting resized (or scaled to AspectFit), then your aim is to resize the imageview to the new resized image.

如果您有一个固定大小的 ImageView,其中动态加载的图像正在调整大小(或缩放到 AspectFit),那么您的目标是将 imageview 调整为新的调整大小的图像。

The shortest way to do this:

执行此操作的最短方法:

// 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)];

But to use the AVMakeRectWithAspectRatioInsideRect, you need to add this

但是要使用 AVMakeRectWithAspectRatioInsideRect,你需要添加这个

#import <AVFoundation/AVFoundation.h>

import statement to your file and also include the AVFoundation framework in your project (comes bundled with the SDK).

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

回答by Andrew Johnson

You can't add a border, but this would work for the same effect. You could also make the UIView called blackBG in this example into a UIImageView with a border image and a blank middle, and then you'd have a custom image border instead of just black.

您不能添加边框,但这会产生相同的效果。您还可以将本示例中名为 blackBG 的 UIView 制作为带有边框图像和空白中间的 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];

回答by ingconti

all these answers work fine BUT add a rect to an image. Suppose You have a shape (in my case a butterfly) and You want to add a border (a red border):

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

we need two steps: 1) take the image, convert to CGImage, pass to a function to draw offscreen in a context using CoreGraphics, and give back a new CGImage

我们需要两个步骤:1)获取图像,转换为 CGImage,传递给一个函数以使用 CoreGraphics 在上下文中绘制离屏,并返回一个新的 CGImage

2) convert to uiimage back and draw:

2)转换为uiimage返回并绘制:

// 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];

}

}

I added a normal butterfly and a red-bordered bigger butterfly.

我添加了一只普通蝴蝶和一只红色边框的大蝴蝶。

回答by poleB

You can add border to the UIImageView, and then change the size of the UIimageView according to the image size:

可以给 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;

Make sure you to set the origin of the imageView to the center

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

回答by Kendall Helmstetter Gelner

You could manipulate the image itself, but a much better way is to simply add a UIView that contains the UIImageView, and change the background to black. Then set the size of that container view to a little bit larger than the UIImageView.

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

回答by Luca Davanzo

Another way is to do directly from designer.

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

Select your image and go under "Show the Identity inspector".

选择您的图像并进入“显示身份检查器”。

Here you can manually add "User Defined Runtime Attributes":

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

layer.borderColor
layer.borderWidth



enter image description here

在此处输入图片说明