我有这个代码:
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)
因此,我收到蓝色背景而不是图片的警报。为什么会这样?
在 swift 中尝试以下代码
saveAction.setValue(UIImage(named: "name.png").withRenderingMode(UIImageRenderingMode.alwaysOriginal), forKey: "image")
或者试试这个
saveAction.setValue(zdjecieGlowne.withRenderingMode(UIImageRenderingMode.alwaysOriginal), forKey: "image")
请尝试
[saveAction setValue:[[UIImage imageNamed:@"image-name.jpg"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forKey:@"image"];
在斯威夫特中:
saveAction.setValue(UIImage(named: "image-name.jpg").withRenderingMode(UIImageRenderingMode.alwaysOriginal), forKey: "image")
您可以像下面的代码一样添加图像
var imageView = UIImageView(frame: CGRectMake(220, 10, 40, 40))
imageView.image = yourImage
alert.view.addSubview(imageView)