Iphone 如何拍摄UIImage并给它加上黑色边框?
如何设置Iphone 如何拍摄UIImage并给它加上黑色边框?,iphone,ios,objective-c,uiimageview,uiimage,Iphone,Ios,Objective C,Uiimageview,Uiimage,如何设置UIImage的边框?您可以操纵图像本身,但更好的方法是简单地添加包含UIImageView的UIView,并将背景更改为黑色。然后将容器视图的大小设置为比UIImageView大一点。您不能添加边框,但这会产生相同的效果。在本例中,您还可以将名为BlackG的UIView制作成一个带有边框图像和中间空白的UIImageView,然后您将拥有一个自定义图像边框,而不仅仅是黑色 UIView *blackBG = [[UIView alloc] initWithFrame:CGRectMa
UIImage
的边框?您可以操纵图像本身,但更好的方法是简单地添加包含UIImageView的UIView,并将背景更改为黑色。然后将容器视图的大小设置为比UIImageView大一点。您不能添加边框,但这会产生相同的效果。在本例中,您还可以将名为BlackG的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];
您可以通过创建一个新图像来实现这一点(在您的其他帖子中也回答了这个问题):
此代码将在图像周围生成粉红色边框。但是,如果要仅显示边框,请使用
UIImageView的图层设置边框。使用OS>3.0可以执行以下操作:
//you need this import
#import <QuartzCore/QuartzCore.h>
[imageView.layer setBorderColor: [[UIColor blackColor] CGColor]];
[imageView.layer setBorderWidth: 2.0];
//您需要此导入
#进口
[imageView.layer setBorderColor:[[UIColor blackColor]CGColor]];
[imageView.layer-Width:2.0];
您可以向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添加边框
[imageView.layer setBorderColor:[[UIColor whiteColor]CGColor]];
[imageView.layer-Width:2.0];
//调整imageView的大小以适合图像大小
CGSize size=[图像大小];
浮动系数=size.width/self.frame.size.width;
if(系数<尺寸高度/自身框架尺寸高度){
系数=size.height/self.frame.size.height;
}
CGRect rect=CGRectMake(0,0,size.width/因子,size.height/因子);
imageView.frame=rect;
确保将imageView的原点设置为中心
#import <QuartzCore/CALayer.h>
UIImageView *imageView = [UIImageView alloc]init];
imageView.layer.masksToBounds = YES;
imageView.layer.borderColor = [UIColor blackColor].CGColor;
imageView.layer.borderWidth = 1;
UIImageView*imageView=[UIImageView alloc]init];
imageView.layer.masksToBounds=是;
imageView.layer.borderColor=[UIColor blackColor].CGColor;
imageView.layer.borderWidth=1;
此代码可用于添加
UIImageView
视图边框。所有这些答案都可以正常工作,但会向图像添加一个rect。
假设您有一个形状(在我的例子中是蝴蝶),并且希望添加边框(红色边框):
我们需要两个步骤:
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;
}
}
我添加了一只普通蝴蝶和一只红色边框的大蝴蝶。如果您知道图像的尺寸,那么在UIImageView的图层上添加边框是目前最好的解决方案。实际上,您可以简单地将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)];
但是要使用avmakerect和aspectratioinsidect,您需要添加以下内容
#import <AVFoundation/AVFoundation.h>
#导入
将语句导入到文件中,并在项目中包含AVFoundation framework(随SDK附带)。此函数将返回带有黑边框的图像。请尝试此操作。。希望这对你有帮助
- (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;
}
//你需要导入
QuartzCore/QuartzCore.h
&然后在边框中显示ImageView
[imageView.layer setBorderColor: [[UIColor blackColor] CGColor]];
[imageView.layer setBorderWidth: 2.0];
[imageView.layer setCornerRadius: 5.0];
另一种方法是直接从设计器执行 选择您的图像并进入“显示身份检查器” 您可以在此处手动添加“用户定义的运行时属性”:
layer.borderColor
layer.borderWidth
在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上寻找即插即用解决方案的人,我写了CodyMac的答案作为扩展 用法:
let outline=UIImage(名为:“某物”)?.outline()
我使用此方法在图像外部添加边框。您可以在
boderWidth
常量中自定义边框宽度
斯威夫特3
我创建了一个类,它为imageView h添加了一个边框。使用这个类而不是UIImageView。我已经给了4个填充。你可以按你的意愿给予
class UIBorderImageView: UIView {
private lazy var imageView: UIImageView = {
let imageView = UIImageView()
imageView.contentMode = .scaleAspectFit
imageView.translatesAutoresizingMaskIntoConstraints = false
return imageView
}()
override init(frame: CGRect) {
super.init(frame: frame)
self.backgroundColor = UIColor.White()
self.layer.borderColor = UIColor.GreyMedium().cgColor
self.layer.borderWidth = 1.0
self.layer.cornerRadius = 4.0
self.layer.masksToBounds = true
self.setUpViews()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
private func setUpViews(){
self.addSubview(imageView)
self.addConstraintsWithFormat(format: "H:|-4-[v0]-4-|", views: imageView)
self.addConstraintsWithFormat(format: "V:|-4-[v0]-4-|", views: imageView)
}
func configureImageViewWith(image:UIImage){
self.imageview.image = image
}}
它是否可以处理动态变化的不同大小的图像?UIImageView将一直更改,而UIView?p、 操作图像本身会很好。你必须调整包含视图的框架以及图像-你可以保存两个视图的中心属性,调整图像大小,调整容器大小,然后为这两个视图重新设置中心属性。这正是我所做的,而不是用CG笔划路径。这似乎更容易。当我在上面的图像视图中对图像进行aspectFit时,图像可以完美地查看,但图像的侧面是空白的,当图像为横向时,图像的上下部分也是如此。空白空间看起来很丑陋,边界设置了它。你面对过这个问题吗?如果是,请建议解决方法this@HamutsiimageView.image=myImage;是的,你可以。可以使用CoreGraphics将
UIImage
的实例绘制到图形上下文中。正如@rockstar指出的,您可能需要添加imageView.layer.masksToBounds=YES;这就是我一直在寻找的解决方案。你如何指定你想要的边框宽度?@MarcusS.Zarra回答这个问题时,我意识到它们并不存在,但这个解决方案没有考虑视网膜显示。如果你能修改的话,我将非常感激:)@lukech样品不需要修改。如果你在视网膜设备上,只需增加线宽。大小
是320x480,无论你是否在视网膜设备上,因此当你保存图像时,它以320x480px的分辨率保存,而不是640x960。现在正在处理它,但我如何才能绕过这个边界的拐角?这就是我最后要做的;将myUIImageView
嵌套在框架颜色稍大的UIView
的中心。如果您发现任何问题
imageView_ProfileImage.layer.cornerRadius =10.0f;
imageView_ProfileImage.layer.borderColor = [[UIColor blackColor] CGColor];
imageView_ProfileImage.layer.borderWidth =.4f;
imageView_ProfileImage.layer.masksToBounds = YES;
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
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
}
}
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;
}
class UIBorderImageView: UIView {
private lazy var imageView: UIImageView = {
let imageView = UIImageView()
imageView.contentMode = .scaleAspectFit
imageView.translatesAutoresizingMaskIntoConstraints = false
return imageView
}()
override init(frame: CGRect) {
super.init(frame: frame)
self.backgroundColor = UIColor.White()
self.layer.borderColor = UIColor.GreyMedium().cgColor
self.layer.borderWidth = 1.0
self.layer.cornerRadius = 4.0
self.layer.masksToBounds = true
self.setUpViews()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
private func setUpViews(){
self.addSubview(imageView)
self.addConstraintsWithFormat(format: "H:|-4-[v0]-4-|", views: imageView)
self.addConstraintsWithFormat(format: "V:|-4-[v0]-4-|", views: imageView)
}
func configureImageViewWith(image:UIImage){
self.imageview.image = image
}}