收到此错误消息Swift 4.2:实例成员'jpegData'不能用于类型'UIImage';您是要使用这种类型的值吗?

时间:2018-12-05 00:56:05

标签: ios swift4.2

func uploadGoogleProfileImage(_ image:UIImage, completion: @escaping ((_ url:URL?)->())) {
    guard let uid = Auth.auth().currentUser?.uid else { return }
    let storageRef = Storage.storage().reference().child("userGoogleImage/\(uid)")

    guard let imageData = UIImage.jpegData(compressionQuality: 0.75) else { return }

    let metaData = StorageMetadata()
    metaData.contentType = "image/jpg"

    storageRef.putData(imageData, metadata: metaData) { metaData, error in
        if error == nil, metaData != nil {

            storageRef.downloadURL{ url, error in
                completion(url)
            }
        } else {
            // failed
            completion(nil)
        }
    }
}

该错误显示在使用UIImage.jpegData的行上。不知道为什么会收到此错误?

1 个答案:

答案 0 :(得分:0)

jpegData已更改为实例方法而不是类方法。如此改变

guard let imageData = UIImage.jpegData(compressionQuality: 0.75) else { return }

guard let data = image.jpegData(compressionQuality: 0.75) else { return }

可以解决问题。