ios - UIImageJPEGRepresentation 已被实例方法 UIImage.jpegData(compressionQuality :)

标签 ios swift swift4 ios12

我尝试将照片上传到 Firebase,但出现此错误。它在 Xcode 10 之前工作。我收到此错误:

'UIImageJPEGRepresentation' has been replaced by instance method '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)
            }
        }
    }
}

最佳答案

该错误告诉您,从 iOS 12 开始,旧的 UIImageJPEGRepresentation 函数已被新的 jpegData 取代。 UIImage 上的方法。

改变:

let imageData = UIImageJPEGRepresentation(image, 0.75)

至:

let imageData = image.jpegData(compressionQuality: 0.75)

同样,UIImagePNGRepresentation 的使用已替换为 pngData()

关于ios - UIImageJPEGRepresentation 已被实例方法 UIImage.jpegData(compressionQuality :),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55537945/

相关文章:

Swift 泛型 - 协议(protocol)不符合协议(protocol)

ios - 应用程序未以模态方式呈现,但崩溃并显示 `Application tried to present modally an active controller`

ios - Root View Controller Swift

iphone - 在 Iphone 中使用页面控件滑动图像

ios - 滚动后表格 View 单元格困惑

ios - 在 iOS 设备上找不到 libSwiftCore.dylib 图像错误

xcode - "cacheParamsComputed"在 Swift 4 中意味着什么?

swift - ImplicitlyUnwrappedOptional in init vs later

ios - 在Selenium Grid中通过Appium测试时,如何在硬件上安装iOS应用?

iphone - 将 UISearchBar 锁定在 TableView 之上