Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 506 Vote(s) - 3.52 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Add image to alert view

#1
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)
Reply

#2
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)
}
}
Reply

#3


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: {
})
}
}
Reply

#4
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]:
Reply

#5
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)
Reply

#6

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)
Reply

#7
Swift 4:


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

Reply



Forum Jump:


Users browsing this thread:
2 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through