【问题标题】:Error in Creating a Rounded Image of Equal Height and Width for Different iOS Devices为不同的 iOS 设备创建等高和等宽的圆形图像时出错
【发布时间】:2020-07-29 13:48:16
【问题描述】:

我创建了一个圆形图像视图,以使用相机或从照片库中捕捉一个人的图像。我已将图像放置在带有文本字段和标签的堆栈视图中。当我为图像设置相同高度和宽度的约束(常量)时,图像显示为圆形。但是,当我为不同的 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
}

}

【问题讨论】:

标签: ios swift xcode


【解决方案1】:

而不是viewDidLoad() 调用viewDidLayoutSubviews() 中的舍入函数

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

【讨论】:

    猜你喜欢
    • 2021-05-25
    • 1970-01-01
    • 2010-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-15
    • 1970-01-01
    相关资源
    最近更新 更多