0Day Forums
Add image to alert view - Printable Version

+- 0Day Forums (https://zeroday.vip)
+-- Forum: Coding (https://zeroday.vip/Forum-Coding)
+--- Forum: Swift (https://zeroday.vip/Forum-Swift)
+--- Thread: Add image to alert view (/Thread-Add-image-to-alert-view)



Add image to alert view - Sircuneus761 - 07-18-2023

I have an alert view that pops up when the user press the add button. How do i add an image to the alert view?

I added some code that i took reference from stack overflow. My save button is replaced with the image and the image appear to be in blue colour...

**Code for Alert View**

var alert = UIAlertController(title: "Spring Element \(springNumber)",
message: "Add spring properties",
preferredStyle: .Alert)

let saveAction = UIAlertAction(title: "Save",
style: .Default) { (action: UIAlertAction!) -> Void in

let textField1 = alert.textFields![0] as UITextField
self.txtField1.append(textField1.text)
self.tableView.reloadData()

let textField2 = alert.textFields![1] as UITextField
self.txtField2.append(textField2.text)
self.tableView.reloadData()

println(self.txtField1)
println(self.txtField2)
}

let cancelAction = UIAlertAction(title: "Cancel",
style: .Default) { (action: UIAlertAction!) -> Void in
}

//adding textfield1
alert.addTextFieldWithConfigurationHandler {
(textField1: UITextField!) -> Void in
textField1.placeholder = "Force"
}

//adding textfield2
alert.addTextFieldWithConfigurationHandler {
(textField2: UITextField!) -> Void in
textField2.placeholder = "Stiffness"
}

alert.addAction(saveAction)
alert.addAction(cancelAction)

presentViewController(alert,
animated: true,
completion: nil)

**Code for Image View**


let image = UIImage(named: "springAtWall")
saveAction.setValue(image, forKey: "image")
alert.addAction(saveAction)


RE: Add image to alert view - Mrdiametrically6 - 07-18-2023

It wasn't immediately clear to me what @Kakumanu's ImageContext stuff was for, but that's to resize the UIImage. Perhaps a slightly more Swifty way of doing it would be through a UIImage extension ```


extension UIImage {
/// Resize a UIImage
func imageWith(newSize: CGSize) -> UIImage {
let renderer = UIGraphicsImageRenderer(size: newSize)
let image = renderer.image { _ in
self.draw(in: CGRect.init(origin: CGPoint.zero, size: newSize))
}
return image.withRenderingMode(self.renderingMode)
}
}


RE: Add image to alert view - hobbism736 - 07-19-2023



func launchAlertTool(image: UIImage, topViewController: UIViewController) {
let alert = UIAlertController(title: "Add Comments",
message: nil,
preferredStyle: .alert)

let subview = (alert.view.subviews.first?.subviews.first?.subviews.first!)! as UIView
subview.backgroundColor = UIColor(red: (145/255.0), green: (200/255.0), blue: (0/255.0), alpha: 1.0)
let imageView = UIImageView(frame: CGRect(x: 0, y: 100, width: topViewController.view.bounds.width, height: topViewController.view.bounds.width - 150))
imageView.image = image
imageView.contentMode = .scaleAspectFit
alert.view.addSubview(imageView)
alert.view.tintColor = .black

let height = NSLayoutConstraint(item: alert.view as Any, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: topViewController.view.bounds.width)
let width = NSLayoutConstraint(item: alert.view as Any, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: topViewController.view.bounds.width)
alert.view.addConstraint(height)
alert.view.addConstraint(width)

alert.addTextField { (textField) in
// optionally configure the text field
textField.keyboardType = .alphabet
textField.textAlignment = .center
textField.tintColor = .systemPink
}

let okAction = UIAlertAction(title: "Share", style: .default) { [unowned alert] (action) in
if let textField = alert.textFields?.first {
let comment = textField.text ?? "Check this image"
let shareAll = [comment, screenShot] as [Any]
let activityViewController = UIActivityViewController(activityItems: shareAll, applicationActivities: nil)
activityViewController.popoverPresentationController?.sourceView = topViewController.view
topViewController.navigationController?.present(activityViewController, animated: true, completion: {
})
}
}
alert.addAction(okAction)
if let topVC = UIApplication.getTopViewController() {
topVC.present(alert, animated: true, completion: {
})
}
}


RE: Add image to alert view - plumage722 - 07-19-2023

Here is the solution for Swift 4:

let showAlert = UIAlertController(title: "Demo Alert", message: nil, preferredStyle: .alert)
let imageView = UIImageView(frame: CGRect(x: 10, y: 50, width: 250, height: 230))
imageView.image = image // Your image here...
showAlert.view.addSubview(imageView)
let height = NSLayoutConstraint(item: showAlert.view, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 320)
let width = NSLayoutConstraint(item: showAlert.view, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 250)
showAlert.view.addConstraint(height)
showAlert.view.addConstraint(width)
showAlert.addAction(UIAlertAction(title: "OK", style: .default, handler: { action in
// your actions here...
}))
self.present(showAlert, animated: true, completion: nil)

Output will be somehow like below for all iPhones:<br/><br/>
[![Alert with Image][1]][1]


[1]:



RE: Add image to alert view - serpasil359218 - 07-19-2023

Yes, you can add a `UIImageView` as a subview to your alert view.

var imageView = UIImageView(frame: CGRect(x: 220, y: 10, width: 40, height: 40))
imageView.image = yourImage

alert.view.addSubview(imageView)


RE: Add image to alert view - creon836 - 07-19-2023


We can add image as one option in alert view controller like this.

let imageView = UIImageView(frame: CGRect(origin: CGPoint(x: 0,y :0), size: CGSize(width: 196, height: 196)))
imageView.image = image

UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, imageView.isOpaque, 0.0)
defer { UIGraphicsEndImageContext() }
let context = UIGraphicsGetCurrentContext()
imageView.layer.render(in: context!)
let finalImage = UIGraphicsGetImageFromCurrentImageContext()

let alertMessage = UIAlertController(title: "Your Title", message: "", preferredStyle: .alert)
let action = UIAlertAction(title: "", style: .default, handler: nil)
action.setValue(finalImage?.withRenderingMode(UIImageRenderingMode.alwaysOriginal), forKey: "image")
alertMessage .addAction(action)
let action1 = UIAlertAction(title: "OK", style: .default, handler: nil)
alertMessage .addAction(action1)

self.present(alertMessage, animated: true, completion: nil)


RE: Add image to alert view - Sirselineowttdjjjt - 07-19-2023

Swift 4:


var imageView = UIImageView(frame: CGRect(x: 220, y: 10, width: 40, height: 40))
imageView.image = <#yourImage#>
alert.view.addSubview(imageView)