use*_*888 47 uiimageview ios swift xcode6
我希望将图片显示为imageView,如图像联系人(在一个圆圈中)但是当我尝试显示这个时,imageView会重新调整他的大小,这在圆圈中无法正确显示.
image.layer.borderWidth=1.0
image.layer.masksToBounds = false
image.layer.borderColor = UIColor.whiteColor().CGColor
image.layer.cornerRadius = image.frame.size.height/2
image.clipsToBounds = true
Run Code Online (Sandbox Code Playgroud)
我想要这样的节目:

但我明白了:

如何将该图像调整为UIImageView大小以显示为圆圈?
谢谢!
Man*_*pta 41
这是我在我的应用中使用的解决方案:
var image: UIImage = UIImage(named: "imageName")
imageView.layer.borderWidth = 1.0
imageView.layer.masksToBounds = false
imageView.layer.borderColor = UIColor.whiteColor().CGColor
imageView.layer.cornerRadius = image.frame.size.width/2
imageView.clipsToBounds = true
Run Code Online (Sandbox Code Playgroud)
Swift 4.0
let image = UIImage(named: "imageName")
imageView.layer.borderWidth = 1.0
imageView.layer.masksToBounds = false
imageView.layer.borderColor = UIColor.white.cgColor
imageView.layer.cornerRadius = image.frame.size.width / 2
imageView.clipsToBounds = true
Run Code Online (Sandbox Code Playgroud)
chu*_*kus 40
什么frame是您使用的大小image?如果我将框架设置为正方形,我可以得到一个完美的圆.
let image = UIImageView(frame: CGRectMake(0, 0, 100, 100))
Run Code Online (Sandbox Code Playgroud)
fat*_*han 18
快速和简单的解决方案.
如何掩盖 UIImage金环没有与斯威夫特裁剪.
extension UIImageView {
public func maskCircle(anyImage: UIImage) {
self.contentMode = UIViewContentMode.ScaleAspectFill
self.layer.cornerRadius = self.frame.height / 2
self.layer.masksToBounds = false
self.clipsToBounds = true
// make square(* must to make circle),
// resize(reduce the kilobyte) and
// fix rotation.
self.image = anyImage
}
}
Run Code Online (Sandbox Code Playgroud)
怎么称呼:
let anyAvatarImage:UIImage = UIImage(named: "avatar")!
avatarImageView.maskCircle(anyAvatarImage)
Run Code Online (Sandbox Code Playgroud)
试试这个对我有用,
设置imageView 的宽度和高度相同。
迅速
imageview?.layer.cornerRadius = (imageview?.frame.size.width ?? 0.0) / 2
imageview?.clipsToBounds = true
imageview?.layer.borderWidth = 3.0
imageview?.layer.borderColor = UIColor.white.cgColor
Run Code Online (Sandbox Code Playgroud)
目标 C
self.imageView.layer.cornerRadius = self.imageView.frame.size.width / 2;
self.imageView.clipsToBounds = YES;
self.imageView.layer.borderWidth = 3.0f;
self.imageView.layer.borderColor = [UIColor whiteColor].CGColor;
Run Code Online (Sandbox Code Playgroud)
希望这将有助于某人。
如果使用自动布局,则创建自定义圆UIImageView并在layoutSubviews下创建圆。
/*
+-------------+
| |
| |
| |
| |
| |
| |
+-------------+
The IMAGE MUST BE SQUARE
*/
class roundImageView: UIImageView {
override init(frame: CGRect) {
// 1. setup any properties here
// 2. call super.init(frame:)
super.init(frame: frame)
// 3. Setup view from .xib file
}
required init?(coder aDecoder: NSCoder) {
// 1. setup any properties here
// 2. call super.init(coder:)
super.init(coder: aDecoder)
// 3. Setup view from .xib file
}
override func layoutSubviews() {
super.layoutSubviews()
self.layer.borderWidth = 1
self.layer.masksToBounds = false
self.layer.borderColor = UIColor.white.cgColor
self.layer.cornerRadius = self.frame.size.width/2
self.clipsToBounds = true
}
}
Run Code Online (Sandbox Code Playgroud)
那就是你所需要的...
profilepic = UIImageView(frame: CGRectMake(0, 0, self.view.bounds.width * 0.19 , self.view.bounds.height * 0.1))
profilepic.layer.borderWidth = 1
profilepic.layer.masksToBounds = false
profilepic.layer.borderColor = UIColor.blackColor().CGColor
profilepic.layer.cornerRadius = profilepic.frame.height/2
profilepic.clipsToBounds = true
slider.addSubview(profilepic)
Run Code Online (Sandbox Code Playgroud)
小智 5
我建议你的图像文件开始时是一个完美的正方形.这几乎可以在任何照片编辑程序中完成.之后,这应该在viewDidLoad中工作.感谢这个视频
image.layer.cornerRadius = image.frame.size.width/2
image.clipsToBounds = true
Run Code Online (Sandbox Code Playgroud)
如果您使用UIViewController这里是如何使用Anchors。关键是要设置的ImageViewlayer.cornerRadius中viewWillLayoutSubviews像这样:
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
imageView.layer.cornerRadius = imageView.frame.size.width / 2
}
Run Code Online (Sandbox Code Playgroud)
还要确保heightAnchor和widthAnchor的大小相同。他们都100在我下面的例子中
代码:
let imageView: UIImageView = {
let imageView = UIImageView()
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.layer.borderWidth = 1.0
imageView.clipsToBounds = true
imageView.layer.borderColor = UIColor.white.cgColor
return imageView
}()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(imageView)
imageView.heightAnchor.constraint(equalToConstant: 100).isActive = true
imageView.widthAnchor.constraint(equalToConstant: 100).isActive = true
imageView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
imageView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 50).isActive = true
imageView.image = UIImage(named: "pizzaImage")
}
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
imageView.layer.cornerRadius = imageView.frame.size.width / 2
}
Run Code Online (Sandbox Code Playgroud)
如果您使用CollectionView Cellset imageView's layer.cornerRadiusin layoutSubviews():
override init(frame: CGRect) {
super.init(frame: frame)
addSubview(imageView)
imageView.heightAnchor.constraint(equalToConstant: 100).isActive = true
imageView.widthAnchor.constraint(equalToConstant: 100).isActive = true
imageView.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true
imageView.topAnchor.constraint(equalTo: self.topAnchor, constant: 50).isActive = true
imageView.image = UIImage(named: "pizzaImage")
}
override func layoutSubviews() {
super.layoutSubviews() // call super.layoutSubviews()
imageView.layer.cornerRadius = imageView.frame.size.width / 2
}
Run Code Online (Sandbox Code Playgroud)
这个扩展真的对我有用(包括在 swift 4+ 中)
extension UIImageView {
func roundedImage() {
self.layer.cornerRadius = (self.frame.size.width) / 2;
self.clipsToBounds = true
self.layer.borderWidth = 3.0
self.layer.borderColor = UIColor.white.cgColor
}
}
Run Code Online (Sandbox Code Playgroud)
然后简单地将其称为
imageView.roundedImage()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
67768 次 |
| 最近记录: |