我正在制作一个非常类似于Instagram的多图像发布功能的项目,由于ImageUploadManager,我在CapturePhotoController中有两个彼此关联的错误。错误是:
无法分配给'ImageUploadManager.Type'类型的不可变表达式
输入“ImageUploadManager?”没有成员“uploadImage”
这是在此 View Controller 中发生的:*错误带有注释标记
import UIKit
class takePicViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
@IBOutlet weak var imageView: UIImageView!
let imagePicker = UIImagePickerController()
override func viewDidLoad() {
super.viewDidLoad()
imagePicker.delegate = self
imagePicker.sourceType = .camera
imagePicker.allowsEditing = false
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let userPickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
// Error ImageUploadManager = ImageUploadManager()
// Error ImageUploadManager?.uploadImage(userPickedImage, progressBlock: { (percentage) in
print(percentage)
}, completionBlock: { (fileURL, errorMessage) in
print(fileURL)
print(errorMessage)
})
PhotoArray.sharedInstance.photosArray.append(userPickedImage) //append converted data in array
imageView.image = userPickedImage
}
imagePicker.dismiss(animated: true, completion: nil)
}
@IBAction func cameraButtonPressed(_ sender: UIButton) {
present(imagePicker, animated: true, completion: nil)
}
}
这是ImageUploadManager的代码:
import UIKit
import FirebaseStorage
import Firebase
struct Constants {
struct Car {
static let imagesFolder: String = "carImages"
}
}
class ImageUploadManager: NSObject {
func uploadImage(_ image: UIImage, /**/ progressBlock: @escaping (_ percentage: Double) -> Void, /**/ completionBlock: @escaping (_ url: String?, _ errorMessage: String?) -> Void) {
let data = Data()
let storage = Storage.storage()
let storageRef = storage.reference()
let imageName = "\(Date().timeIntervalSince1970).jpg"
let imagesReference = storageRef.child(Constants.Car.imagesFolder).child(imageName)
if let imageData = UIImageJPEGRepresentation(image, 0.8) {
let metadata = storageRef.child("image/jpg")
let uploadTask = metadata.putData(data, metadata: nil) { (metadata, error) in
guard let metadata = metadata else {
// Uh-oh, an error occurred!
return
}
storageRef.downloadURL { (url, error) in
guard let downloadURL = url else {
// Uh-oh, an error occurred!
return
}
}
}
uploadTask.observe(.progress, handler: { (snapshot) in
guard let progress = snapshot.progress else {
return
}
let percentage = (Double(progress.completedUnitCount) / Double(progress.totalUnitCount)) * 100
progressBlock(percentage)
})
} else {
completionBlock(nil, "Image could not be converted to Data.")
}
}
}
最佳答案
是注释的代码:// Error ImageUploadManager = ImageUploadManager()
您的ImageUploadManager
声明?如果是这样,那是完全错误的。应该声明如下:
if let userPickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
let imageUploadManager = ImageUploadManager()
imageUploadManager.uploadImage(userPickedImage, prog.....
读取
ImageUploadManager
似乎是一个无状态对象,您可以将函数声明为class
函数。`class func uploadImage(_ image: UIImage`......`
然后可以调用它:
if let userPickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
ImageUploadManager.uploadImage(userPickedImage, prog.....