I am trying to change the corner radius of an image in the tab bar after updating the image from data retrieved by the url.
I tried implementing layer.cornerRadius in viewDidAppear along with viewDidLayoutSubviews().
The image shows up as expected but cannot get the corner radius to change.
var profileImageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
// check users profile image
if let imageURL = Auth.auth().currentUser?.photoURL {
guard let url = URL(string: imageURL.absoluteString) else { return }
profileImageView.translatesAutoresizingMaskIntoConstraints = false
profileImageView.layer.masksToBounds = true
profileImageView.clipsToBounds = true
let data = try! Data(contentsOf: url)
let image = UIImage(data: data)!
let resized = resizeImage(image: image, targetSize: CGSize(width: 30, height: 30))?.withRenderingMode(.alwaysOriginal)
profileImageView.image = resized
//profileImageView.layer.cornerRadius = 30//( imageView.frame.size.width ) / 2
DispatchQueue.main.async {
self.navigationController?.tabBarController?.tabBar.items![2].image = self.profileImageView.image
}
}
}
func resizeImage(image: UIImage, targetSize: CGSize) -> UIImage? {
let size = image.size
let widthRatio = targetSize.width / size.width
let heightRatio = targetSize.height / size.height
var newSize: CGSize
if(widthRatio > heightRatio) {
newSize = CGSize(width: size.width * heightRatio, height: size.height * heightRatio)
} else {
newSize = CGSize(width: size.width * widthRatio, height: size.height * widthRatio)
}
let rect = CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height)
UIGraphicsBeginImageContextWithOptions(newSize, false, 1.0)
image.draw(in: rect)
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage
}