ios - 如何为ImagePicker制作圆形编辑区域而不是矩形?-LMLPHP我有针对用户的注册表,用户可以在其中添加其照片作为个人资料。我为imageView创建了圆形,但是当我从库中选择照片时,照片没有被四舍五入,并且超出了

class SigninViewController: UIViewController {


var imagePicker = UIImagePickerController()

@IBOutlet var countPlaceInCar: UITextField!
@IBOutlet var nameOfCar: UITextField!

@IBOutlet var AddPhotoButton: UIButton!
@IBOutlet var registerButton: UIButton!

@IBOutlet var registrationSegmented: UISegmentedControl!


@IBOutlet var addPhotoImage: UIImageView!



override func viewDidLoad() {
    super.viewDidLoad()
    makeRounded()
    imagePicker.delegate = self
 }

//func that rounded image for user
func makeRounded() {
    addPhotoImage.layer.borderWidth = 1
    addPhotoImage.layer.masksToBounds = false
    addPhotoImage.layer.borderColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
    addPhotoImage.layer.cornerRadius = addPhotoImage.frame.height/2 //This will change with corners of image and height/2 will make this circle shape

}

@IBAction func addPhotoFromLibraryOrCamera(_ sender: UIButton) {
    imagePicker.sourceType = .photoLibrary
    imagePicker.allowsEditing = true
    present(imagePicker, animated: true, completion: nil)

   }
}

extension SigninViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
            addPhotoImage.image = image


        }
        dismiss(animated: true, completion: nil)
    }
}

最佳答案

只需将masksToBounds设置为true:

func makeRounded() {
    addPhotoImage.layer.borderWidth = 1
    addPhotoImage.layer.borderColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
    addPhotoImage.layer.cornerRadius = addPhotoImage.frame.height/2 //This will change with corners of image and height/2 will make this circle shape
    addPhotoImage.layer.masksToBounds = true
}

关于ios - 如何为ImagePicker制作圆形编辑区域而不是矩形?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56118731/

10-13 01:00