最近将Swift4 升级到最新的 Swift4.2,发现了一些问题,其中就包括 调用相册及拍照 UIImagePickerControllerDelegate 委托不执行,导致获取不到选中的图片并且报如下错误:
[discovery] errors encountered while discovering extensions: Error Domain=PlugInKit Code=13 "query cancelled" UserInfo={NSLocalizedDescription=query cancelled}
应改写为如下方式,方可正常执行的,并且不会报错
//正确的写法
//MARK: - UIImagePickerControllerDelegate、UINavigationControllerDelegate
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
//UIImagePickerControllerOriginalImage 原始图像
//UIImagePickerControllerEditedImage 编辑后的图片(开启编辑该对象才存在)
self.dismiss(animated: true, completion: {
var img:UIImage? = info[UIImagePickerController.InfoKey.originalImage] as? UIImage
if picker.allowsEditing {
img = info[UIImagePickerController.InfoKey.editedImage] as? UIImage
}
self._image = img
})
}
另外,补充一点在Swift4.2中,将UIImage 转换为 Data 类型,具体参考如下:
//compressionQuality:参数为压缩率 取值 0-1 的浮点数
//image:UIImage 对象
let imageData:Data? = UIImage.jpegData(image!)(compressionQuality: K_TAILOR_COMPRESSION_RATIO)!
//类似的还有
let imageData:Data? = UIImage.pngData(image!)()