UIImageJPEGRepresentation 已被实例方法 UIImage.jpegData(compressionQuality:) 替换
Posted
技术标签:
【中文标题】UIImageJPEGRepresentation 已被实例方法 UIImage.jpegData(compressionQuality:) 替换【英文标题】:UIImageJPEGRepresentation has been replaced by instance method UIImage.jpegData(compressionQuality:) 【发布时间】:2019-01-02 23:55:49 【问题描述】:我尝试将照片上传到 Firebase,但它给了我这个错误。它在 Xcode 10 之前工作。我收到此错误:
“UIImageJPEGRepresentation”已被实例方法“UIImage.jpegData(compressionQuality:)”替换
我不知道如何使用这个功能。
import UIKit
import Firebase
class SignUpViewController:UIViewController
@IBOutlet weak var profileImageView: UIImageView!
@IBOutlet weak var tapToChangeProfileButton: UIButton!
var continueButton:RoundedWhiteButton!
var imagePicker:UIImagePickerController!
override func viewDidLoad()
super.viewDidLoad()
continueButton.addTarget(self, action: #selector(handleSignUp), for:
.touchUpInside)
let imageTap = UITapGestureRecognizer(target: self, action:
#selector(openImagePicker))
profileImageView.isUserInteractionEnabled = true
profileImageView.addGestureRecognizer(imageTap)
profileImageView.layer.cornerRadius = profileImageView.bounds.height / 2
profileImageView.clipsToBounds = true
imagePicker = UIImagePickerController()
imagePicker.allowsEditing = true
imagePicker.sourceType = .photoLibrary
imagePicker.delegate = self
func uploadProfileImage(_ image:UIImage, completion: @escaping ((_ url:URL?)->()))
guard let uid = Auth.auth().currentUser?.uid else return
let storageRef = Storage.storage().reference().child("user/\(uid)")
guard let imageData = UIImageJPEGRepresentation(image, 0.75) else return
let metaData = StorageMetadata()
metaData.contentType = "image/jpg"
storageRef.putData(imageData, metadata: metaData) metaData, error in
if error == nil, metaData != nil
if let url = metaData?.downloadURL()
completion(url)
else
completion(nil)
// success!
else
// failed
completion(nil)
【问题讨论】:
【参考方案1】:错误告诉您,从 ios 12 开始,旧的 UIImageJPEGRepresentation
函数已被 UIImage
上的新 jpegData
方法替换。
变化:
let imageData = UIImageJPEGRepresentation(image, 0.75)
到:
let imageData = image.jpegData(compressionQuality: 0.75)
同样,UIImagePNGRepresentation
的使用已替换为 pngData()
。
【讨论】:
【参考方案2】:只需替换
guard let imageData = UIImageJPEGRepresentation(image, 0.75) else return
与:
guard let imageData = image.jpegData(compressionQuality: 0.75) else return
【讨论】:
【参考方案3】:这个错误发生在ios 12和swift 4.2版本。
let image = UIImage()
let imageData = UIImageJPEGRepresentation(image, 1)
to:
let image = UIImage()
let imageData = image.jpegData(compressionQuality: 0.50)
你想这样改变。请试试这个它对我有用。
【讨论】:
以上是关于UIImageJPEGRepresentation 已被实例方法 UIImage.jpegData(compressionQuality:) 替换的主要内容,如果未能解决你的问题,请参考以下文章
UIImageJPEGRepresentation 使用大量内存(Swift 3.0)
数据到 UIImage 到 UIImageJPEGRepresentation 相等失败
使用 UIImageJPEGRepresentation 保存图像并用作 UIImage
为啥使用 UIImageJPEGRepresentation 和通过 localpath 获取图像之间的相同图像的数据大小/长度不同
使用 UIImageJPEGRepresentation 时,多少数字保留全分辨率?
UIImageJPEGRepresentation 已被实例方法 UIImage.jpegData(compressionQuality:) 替换