gpt4 book ai didi

swift - 图像未显示在 uiimageview 中

转载 作者:行者123 更新时间:2023-11-28 14:04:09 25 4
gpt4 key购买 nike

我想从库中获取图片,但是在我从库中选择之前图片不会显示

这是我的代码

@IBAction func btnProfile(_ sender: UIButton) {
let alert = UIAlertController(title: "Change Profil Image", message: "Pilih Foto", preferredStyle: UIAlertControllerStyle.actionSheet)
alert.addAction(UIAlertAction(title: "Gunakan Kamera", style: .default, handler: { (action: UIAlertAction!) in
print("Gunakan Kamera")
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera){
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.camera;
imagePicker.allowsEditing = false
self.present(imagePicker, animated: true, completion: nil)
}
}))
alert.addAction(UIAlertAction(title: "Ambil Foto dari Galery", style: .default, handler: { (action: UIAlertAction!) in
print("Ambil Foto dari Galery")
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.photoLibrary){
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary;
imagePicker.allowsEditing = true
self.present(imagePicker, animated: true, completion: nil)

}
}))
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { (action: UIAlertAction!) in
print("Cancel")
}))
present(alert, animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {

guard let image = info[.originalImage] as? UIImage else { return } // error in here >> 'UIImage?' is not convertible to 'UIImage' >> and warning yellow >> Cast from 'Slice<Dictionary<UIImagePickerController.InfoKey, Any>>' (aka 'Slice<Dictionary<NSString, Any>>') to unrelated type 'UIImage' always fails

picker.dismiss(animated: true, completion: nil)

imgProf.image = image
}

func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
dismiss(animated: true, completion: nil)
}

我想在 UIimageview 上显示图像,但是从库中检索图像后,它并不总是出现,guard let 图像错误,并在其中发出黄色警告我使用 swift 3

这是 UIImagePickerControllerDelegate

public protocol UIImagePickerControllerDelegate : NSObjectProtocol {


@available(iOS 2.0, *)
optional public func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any])

@available(iOS 2.0, *)
optional public func imagePickerControllerDidCancel(_ picker: UIImagePickerController)
}

最佳答案

我试过这段代码,它成功了

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {

var selectedImageFromPicker: UIImage?
if let editedImage = info[UIImagePickerControllerEditedImage] as? UIImage {
selectedImageFromPicker = editedImage
} else if let originalImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
selectedImageFromPicker = originalImage
}

if let selectedImage = selectedImageFromPicker {
imgProf.image = selectedImage
}
dismiss(animated: true, completion: nil)
}

感谢@Kamran,你对改变立场的回答是正确的

关于swift - 图像未显示在 uiimageview 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53200311/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com