Reputation: 1863
I have used the following code in my view controller to display the image either from a camera or from the photo library in the circular form. But, I am not getting the desired result as there image is not filling up the entire circle. I have showed my code and the screenshot of the resulting image below. Appreciate your help! Thanks!
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var image1: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
image1.makeRounded()
}
}
//MARK:- Image Picker
extension ViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
//This is the tap gesture added on my UIImageView.
@IBAction func didTapOnImageView(sender: UITapGestureRecognizer) {
//call Alert function
showAlert()
}
//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))
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
present(imagePickerController, animated: true, completion: nil)
}
}
//MARK:- UIImagePickerViewDelegate.
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
dismiss(animated: true) { [weak self] in
guard let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage else { return }
//Setting image to your image view
self?.image1.image = image
}
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
picker.dismiss(animated: true)
}
}
extension UIImageView {
func makeRounded() {
layer.borderWidth = 1
layer.masksToBounds = false
layer.borderColor = UIColor.black.cgColor
layer.cornerRadius = frame.height / 2
clipsToBounds = true
}
}
Upvotes: 1
Views: 189
Reputation: 121
extension UIImageView {
func makeRounded() {
self.layer.borderWidth = 1
self.layer.masksToBounds = false
self.layer.borderColor = UIColor.black.cgColor
self.layer.cornerRadius = self.frame.height / 2
self.clipsToBounds = true
self.contentMode = .scaleAspectFill
}
}
Upvotes: 1
Reputation: 2922
It because your image is not square
You need to change your imageView content mode to aspect fill in the xib or storyboard
or you can do it by code too
imageView.contentMode = .scaleAspectFill
Upvotes: 1