问题
I have this code:
var zdjecieGlowne: UIImage? = nil
let alert = UIAlertController(title:"MyTitle", message: "My Message Box ", preferredStyle: UIAlertControllerStyle.alert)
let saveAction = UIAlertAction(title: "MyTitle", style: .default, handler: nil)
//saveAction.setValue(UIImage(named: "logo")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal), forKey: "image")
saveAction.setValue(zdjecieGlowne, forKey: "image")
dump(zdjecieGlowne)
alert.addAction(saveAction)
alert.addAction(UIAlertAction(title: "Option 1", style: UIAlertActionStyle.default, handler: { alertAction in
//alert.dismiss(animated: true, completion: nil)
print("1 pressed")
}))
alert.addAction(UIAlertAction(title: "Option 2", style: UIAlertActionStyle.default, handler: { alertAction in
//alert.dismiss(animated: true, completion: nil)
print("2 pressed")
}))
alert.addAction(UIAlertAction(title: "Option 3", style: UIAlertActionStyle.default, handler: { alertAction in
//alert.dismiss(animated: true, completion: nil)
print("3 pressed")
}))
self.present(alert, animated: true, completion: nil)
As a result, I receive Alert with a blue background instead of a picture. Why is it like that?
回答1:
Try the below code in swift
saveAction.setValue(UIImage(named: "name.png").withRenderingMode(UIImageRenderingMode.alwaysOriginal), forKey: "image")
or try this
saveAction.setValue(zdjecieGlowne.withRenderingMode(UIImageRenderingMode.alwaysOriginal), forKey: "image")
回答2:
Please try
[saveAction setValue:[[UIImage imageNamed:@"image-name.jpg"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forKey:@"image"];
In Swift:
saveAction.setValue(UIImage(named: "image-name.jpg").withRenderingMode(UIImageRenderingMode.alwaysOriginal), forKey: "image")
回答3:
You can add your image like the below code
var imageView = UIImageView(frame: CGRectMake(220, 10, 40, 40))
imageView.image = yourImage
alert.view.addSubview(imageView)
来源:https://stackoverflow.com/questions/49770018/swift-uialert-with-image