为不同的iOS设备创建相同高度和宽度的圆形图像时出错



我创建了一个圆形图像视图,用相机或照片库捕捉一个人的图像。我已经将图像放置在带有文本字段和标签的堆栈视图中。当我为图像设置相同高度和宽度的约束(常数(时,图像显示为圆形。但是,当我为不同的iOS设备调整图像大小(高度和宽度相等(时,图像似乎不再是一个完美的圆圈,甚至认为我已经将高度和宽度设置为相等。有人能告诉我我能做些什么来解决这个问题吗?谢谢你的帮助!

import UIKit
class ViewController: UIViewController {
@IBOutlet weak var person1ImageView: UIImageView!
@IBOutlet weak var person2ImageView: UIImageView!
var last = 0
override func viewDidLoad() {
super.viewDidLoad()
person1ImageView.makeRounded()
person2ImageView.makeRounded()

person1ImageView.tag = 1
person2ImageView.tag = 2

// Do any additional setup after loading the view.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destination.
// Pass the selected object to the new view controller.
}
*/

}

extension ViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
//This is the tap gesture added on my UIImageView.
@IBAction func didTapOnImageView(sender: UITapGestureRecognizer) {
//call Alert function
self.showAlert()
last = 2
}
@IBAction func didTapOnImageView1(sender: UITapGestureRecognizer) {
//call Alert function
self.showAlert()
last = 1
}
//Show alert to selected the media source type.
private func showAlert() {
let alert = UIAlertController(title: "Image Selection", message: "From where you want to pick this image?", preferredStyle: .actionSheet)
alert.addAction(UIAlertAction(title: "Camera", style: .default, handler: {(action: UIAlertAction) in
self.getImage(fromSourceType: .camera)
}))
alert.addAction(UIAlertAction(title: "Photo Album", style: .default, handler: {(action: UIAlertAction) in
self.getImage(fromSourceType: .photoLibrary)
}))
alert.addAction(UIAlertAction(title: "Cancel", style: .destructive, handler: nil))
self.present(alert, animated: true, completion: nil)
}
//get image from source type
private func getImage(fromSourceType sourceType: UIImagePickerController.SourceType) {
//Check is source type available
if UIImagePickerController.isSourceTypeAvailable(sourceType) {
let imagePickerController = UIImagePickerController()
imagePickerController.delegate = self
imagePickerController.sourceType = sourceType
self.present(imagePickerController, animated: true, completion: nil)
}
}
//MARK:- UIImagePickerViewDelegate.
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
self.dismiss(animated: true) { [weak self] in
guard let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage else { return }
//Setting image to your image view

if (self?.last == self?.person1ImageView.tag) {

self?.person1ImageView.image = image

}
else {

self?.person2ImageView.image = image

}

}
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
picker.dismiss(animated: true, completion: nil)
}

}

extension UIImageView {
func makeRounded() {
self.layer.borderWidth = 3
self.layer.masksToBounds = false
self.layer.borderColor = UIColor.black.cgColor
self.layer.cornerRadius = self.frame.height / 2
self.clipsToBounds = true
self.contentMode = .scaleAspectFill
}

}

代替viewDidLoad()调用viewDidLayoutSubviews()中的舍入函数

override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
person1ImageView.makeRounded()
person2ImageView.makeRounded()
}

最新更新