mcl*_*lin 241
OS> 3.0时,您可以这样做:
//you need this import
#import <QuartzCore/QuartzCore.h>
[imageView.layer setBorderColor: [[UIColor blackColor] CGColor]];
[imageView.layer setBorderWidth: 2.0];
Run Code Online (Sandbox Code Playgroud)
Mar*_*rra 67
您可以通过创建新图像来完成此操作(在此问题的其他帖子中也会回答):
- (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;
}
Run Code Online (Sandbox Code Playgroud)
此代码将在图像周围生成粉红色边框.但是,如果您要显示边框,则使用图层UIImageView并设置其边框.
Fai*_*fai 38
#import <QuartzCore/CALayer.h>
UIImageView *imageView = [UIImageView alloc]init];
imageView.layer.masksToBounds = YES;
imageView.layer.borderColor = [UIColor blackColor].CGColor;
imageView.layer.borderWidth = 1;
Run Code Online (Sandbox Code Playgroud)
此代码可用于添加UIImageView视图边框.
小智 13
imageView_ProfileImage.layer.cornerRadius =10.0f;
imageView_ProfileImage.layer.borderColor = [[UIColor blackColor] CGColor];
imageView_ProfileImage.layer.borderWidth =.4f;
imageView_ProfileImage.layer.masksToBounds = YES;
Run Code Online (Sandbox Code Playgroud)
Sco*_*2k5 11
如果您知道图像的尺寸,那么为UIImageView图层添加边框是最佳解决方案AFAIK.事实上,您可以简单地将您的imageView设置为x,y,image.size.width,image.size.height
如果您有一个固定大小的ImageView,动态加载的图像正在调整大小(或缩放到AspectFit),那么您的目标是将imageview的大小调整为新调整大小的图像.
最简单的方法:
// 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)];
Run Code Online (Sandbox Code Playgroud)
但是要使用AVMakeRectWithAspectRatioInsideRect,您需要添加它
#import <AVFoundation/AVFoundation.h>
Run Code Online (Sandbox Code Playgroud)
将语句导入到您的文件中,并在项目中包含AVFoundation框架(与SDK捆绑在一起).
您无法添加边框,但这可以起到相同的效果.你也可以在这个例子中将名为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];
Run Code Online (Sandbox Code Playgroud)
所有这些答案都很好但是为图像添加了一个矩形.假设你有一个形状(在我的例子中是蝴蝶)并且你想要添加一个边框(一个红色边框):
我们需要两个步骤:1)拍摄图像,转换为CGImage,传递给一个函数,使用CoreGraphics在上下文中绘制屏幕,并返回一个新的CGImage
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;
Run Code Online (Sandbox Code Playgroud)
}
- (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];
Run Code Online (Sandbox Code Playgroud)
}
我添加了一只普通的蝴蝶和一只红色的大蝴蝶.
小智 5
您可以向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;
Run Code Online (Sandbox Code Playgroud)
确保将imageView的原点设置为中心
您可以操纵图像本身,但更好的方法是简单地添加包含 UIImageView 的 UIView,并将背景更改为黑色。然后将该容器视图的大小设置为比 UIImageView 稍大一些。
| 归档时间: |
|
| 查看次数: |
104646 次 |
| 最近记录: |