“ImagePicker”类型的值没有成员“contentMode”
Posted
技术标签:
【中文标题】“ImagePicker”类型的值没有成员“contentMode”【英文标题】:Value of type 'ImagePicker' has no member 'contentMode' 【发布时间】:2021-04-27 12:51:34 【问题描述】:我正在尝试使用下面列出的 contentMode,但我不明白为什么会收到以下错误:
无法根据成员“scaleAspectFit”推断上下文基础
if let uiImage = info[UIImagePickerController.InfoKey.editedImage] as? UIImage
parent.contentMode = .scaleAspectFit <--- Error
parent.image = uiImage
parent.presentationMode.wrappedValue.dismiss()
有什么提示吗? 谢谢!
附上你找到的完整代码:
struct ImagePicker: UIViewControllerRepresentable
@Environment(\.presentationMode) var presentationMode
@Binding var image: UIImage?
func makeUIViewController(context: UIViewControllerRepresentableContext<ImagePicker>) -> UIImagePickerController
let picker = UIImagePickerController()
picker.delegate = context.coordinator
picker.allowsEditing = true
// picker.mediaTypes = ["public.image"]
return picker
func updateUIViewController(_ uiViewController: UIImagePickerController, context: UIViewControllerRepresentableContext<ImagePicker>)
class Coordinator: NSObject, UINavigationControllerDelegate, UIImagePickerControllerDelegate
let parent: ImagePicker
init(_ parent: ImagePicker)
self.parent = parent
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any])
if let uiImage = info[UIImagePickerController.InfoKey.editedImage] as? UIImage
parent.image.contentMode = .scaleAspectFit
parent.image = uiImage
parent.presentationMode.wrappedValue.dismiss()
func makeCoordinator() -> Coordinator
Coordinator(self)
【问题讨论】:
【参考方案1】:添加contentMode
属性
struct ImagePicker: UIViewControllerRepresentable
@Environment(\.presentationMode) var presentationMode
@Binding var image: UIImage?
@Binding var mode:UIView.ContentMode? // here
然后
parent.mode = .scaleAspectFit
【讨论】:
谢谢!这就是解决方案! :-)以上是关于“ImagePicker”类型的值没有成员“contentMode”的主要内容,如果未能解决你的问题,请参考以下文章
“任何”的值类型没有成员 'objectforKey' Swift 3 转换
Swift 3 - 'Any?' 类型的值没有成员“valueForKey”
“String”类型的值没有成员“stringByAppendingPathComponent”[重复]
Xcode 13:`'NSPersistentCloudKitContainerOptions' 类型的值没有成员'databaseScope'`