Swift 4: If the image is larger and it doesn't fit well, use this code and UIImage Extension to resize the image:
func showAlert() {
let alertController = UIAlertController()
let action = UIAlertAction(title: "Title", style: .default) { (action: UIAlertAction!) in
}
if let icon = icon?.imageWithSize(scaledToSize: CGSize(width: 32, height: 32)) {
action.setValue(icon, forKey: "image")
}
alertController.addAction(action)
self.present(alertController, animated: true, completion: nil)
}
And use this extension:
extension UIImage {
func imageWithSize(scaledToSize newSize: CGSize) -> UIImage {
UIGraphicsBeginImageContextWithOptions(newSize, false, 0.0)
self.draw(in: CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height))
let newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
return newImage
}
}