我收到错误“ UIImage?”不能转换为'UIImage'
let selectedImage = info[.originalImage] as? UIImage
行
在我的图像选择器功能中(如下)
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
let selectedImage = info[.originalImage] as? UIImage
// how do i set the image on my buttons when I select a photo?
let imageButton = (picker as? CustomImagePickerController)?.imageButton
imageButton?.setImage(selectedImage?.withRenderingMode(.alwaysOriginal), for: .normal)
dismiss(animated: true)
let filename = UUID().uuidString
let ref = Storage.storage().reference(withPath: "/images/\(filename)")
guard let uploadData = selectedImage?.jpegData(compressionQuality: 0.75) else { return }
//guard let uploadData = selectedImage else { return }
let hud = JGProgressHUD(style: .dark)
hud.textLabel.text = "Uploading image..."
hud.show(in: view)
ref.putData(uploadData, metadata: nil) { (nil, err) in
if let err = err {
hud.dismiss()
print("Failed to upload image to storage:", err)
return
}
print("Finished uploading image")
ref.downloadURL(completion: { (url, err) in
hud.dismiss()
if let err = err {
print("Failed to retrieve download URL:", err)
return
}
print("Finished getting download url:", url?.absoluteString ?? "")
if imageButton == self.image1Button {
self.user?.imageUrl1 = url?.absoluteString
} else if imageButton == self.image2Button {
self.user?.imageUrl2 = url?.absoluteString
} else {
self.user?.imageUrl3 = url?.absoluteString
}
})
}
}
答案 0 :(得分:3)
签名:
<!-- the "span.dot" are now added by "JavaScript" according to the number of "div.news" on the page -->
<!--
added two "data-*" attributes :
data-slideshow-index: the index of "div.news" which the "span.dot" points to (remember! the "span.dots" are added by "JavaScript").
data-slideshow-role: the role (can be either "next" or "prev") of the "a.cycle" element (the last two anchor tags).
-->
<section id="section-three">
<div class="container slideshow-container2">
<div class="news">
<div class="news-content">
<div class="buttons">
<a class="btn company-btn" href="#">COMPANY NEWS</a>
<a class="btn industry-btn" href="#">INDUSTRY NEWS</a>
</div>
<h1>OUR PEOPLE ARE OUT STONGEST ASSET </h1>
</div>
</div>
<div class="news news2">
<div class="news2-content">
<div class="buttons">
<a class="btn company-btn" href="#">COMPANY NEWS</a>
<a class="btn industry-btn" href="#">INDUSTRY NEWS</a>
</div>
<h1 class="media-room">MEDIA ROOM </h1>
</div>
</div>
<!-- the next "div#dots" will be populated by `JavaScript` that adds "span.dot" according to the number of "div.news" in the page -->
<div id="dots"></div>
<!-- added "cycle" class (to be used by "JavaScript" for now) and data-slideshow-role attribute -->
<a href="javascript:void(0);" class="cycle" data-slideshow-role="prev">prev</a>
<a href="javascript:void(0);" class="cycle" data-slideshow-role="next">next</a>
</div>
</section>
已更改为:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])
这是导致错误的原因。
将签名更改为新的签名,即:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any])
将解决该错误。
答案 1 :(得分:2)
遇到相同的错误-展开selectedImage
旁边,请确保您具有正确/更新的函数参数(它们将info
从类型[String : Any]
更改为{{ 1}},这给了我错误。
它应该是这样的:
[UIImagePickerController.InfoKey : Any]
希望这会有所帮助!
答案 2 :(得分:1)
这应该有效:
guard let image = info[UIImagePickerControllerOriginalImage] as? UIImage else { return }
答案 3 :(得分:0)
info: [String : Any]
中的信息已更改(感谢苹果每次都会更改)
在新功能中覆盖您的功能:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
答案 4 :(得分:-1)
那是因为您需要首先解开selectedImage
:
guard let selectedImage = info[.originalImage] as? UIImage else { return }