ios UIImageJPEGRepresentation 已被实例方法 UIImage.jpegData(compressionQuality:) 取代
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/51531165/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UIImageJPEGRepresentation has been replaced by instance method UIImage.jpegData(compressionQuality:)
提问by b3f79
I've tried to upload a photo to Firebase but it's giving me this error. It was working before Xcode 10. I'm getting this error:
我试图将照片上传到 Firebase,但它给了我这个错误。它在 Xcode 10 之前工作。我收到此错误:
'UIImageJPEGRepresentation' has been replaced by instance method 'UIImage.jpegData(compressionQuality:)'
'UIImageJPEGRepresentation' 已被实例方法 'UIImage.jpegData(compressionQuality:)' 取代
and I don't know how to use this function.
我不知道如何使用这个功能。
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)
}
}
}
}
回答by rmaddy
The error is telling you that as of iOS 12 the old UIImageJPEGRepresentation
function has been replaced with the new jpegData
method on UIImage
.
该错误告诉您,从 iOS 12 开始,旧UIImageJPEGRepresentation
函数已替换为 上的新jpegData
方法UIImage
。
Change:
改变:
let imageData = UIImageJPEGRepresentation(image, 0.75)
to:
到:
let imageData = image.jpegData(compressionQuality: 0.75)
Similarly, the use of UIImagePNGRepresentation
has been replaced with pngData()
.
同样, 的使用UIImagePNGRepresentation
已被替换为pngData()
。
回答by stawre
Just replace
只需更换
guard let imageData = UIImageJPEGRepresentation(image, 0.75) else { return }
with:
和:
guard let imageData = image.jpegData(compressionQuality: 0.75) else { return }
回答by Karthickkck
This error occurred in ios 12 and swift 4.2 version.
此错误发生在 ios 12 和 swift 4.2 版本中。
let image = UIImage()
let imageData = UIImageJPEGRepresentation(image, 1)
to:
let image = UIImage()
let imageData = image.jpegData(compressionQuality: 0.50)
you want to change like this. Please try this it's working for me.
你想改变成这样。请试试这个它对我有用。