Swift - 带有图像的UIAlert

时间:2018-04-11 08:20:11

标签: ios iphone swift uialertcontroller

我有这段代码:

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)

因此,我收到带有蓝色背景而非图片的警报。为什么会那样?

3 个答案:

答案 0 :(得分:0)

请尝试

[saveAction setValue:[[UIImage imageNamed:@"image-name.jpg"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forKey:@"image"];

在斯威夫特:

saveAction.setValue(UIImage(named: "image-name.jpg").withRenderingMode(UIImageRenderingMode.alwaysOriginal), forKey: "image")

答案 1 :(得分:0)

在swift中尝试以下代码

       var request = new RestRequest("api/document", Method.POST);

        request.AddFile(Path.GetFileNameWithoutExtension(fullFileName), filepath);
        request.AddParameter("ReferenceType", ReferenceType.ToString());
        request.AddParameter("RefId", StudioEventEntryId.ToString());

        request.AlwaysMultipartFormData = true;

        IRestResponse response = client.Execute(request);

或试试这个

saveAction.setValue(UIImage(named: "name.png").withRenderingMode(UIImageRenderingMode.alwaysOriginal), forKey: "image")

答案 2 :(得分:0)

您可以像下面的代码一样添加图片

var imageView = UIImageView(frame: CGRectMake(220, 10, 40, 40))
imageView.image = yourImage
alert.view.addSubview(imageView)