I switched from utilizing UIViewController Photograph Picker to Swift’s new native Photograph Picker. The picture picker itself works high quality however importing to firebase doesn’t.
I need to compress the picture to not refill my Firebase storage too quick. With the outdated picture picker, I had this code to do this : guard let imageData = Picture.jpegData(compressionQuality: 0.5) else { return } ref.putData(imageData, metadata: nil) { _, error in if let error = error { print("DEBUG: Did not add picture with error: (error.localizedDescription)") return }
however now I can not get the guard let assertion working as a result of “Kind ‘Picture’ has no member ‘jpegData'” Since I am not utilizing UIImage anymore I am unsure the way to go about compressing the picture.
ImageUploader file
`
import PhotosUI
import FirebaseStorage
import SwiftUI
enum UploadType {
case profile
case publish
case story
var filePath: StorageReference {
let filename = NSUUID().uuidString
change self {
case .profile:
return Storage.storage().reference(withPath: "/profile_image/(filename)")
case .publish:
return Storage.storage().reference(withPath: "/post_image/(filename)")
case .story:
return Storage.storage().reference(withPath: "/story_image/(filename)")
}
}} struct ImageUploader {
static func uploadImage(picture: Picture?, sort: UploadType, completion: @escaping(String) -> Void) {
let ref = sort.filePath
guard let imageData = Picture.jpegData(compressionQuality: 0.5) else { return }
ref.putData(imageData, metadata: nil) { _, error in
if let error = error {
print("DEBUG: Did not add picture with error: (error.localizedDescription)")
return
}
ref.downloadURL { imageUrl, _ in
guard let imageUrl = imageUrl?.absoluteString else { return }
completion(imageUrl)
}
}
}
}
`
ImagePicker File – simply in case
`@MainActor
class ImagePicker: ObservableObject {
@Printed var picture: Picture?
@Printed var imageSelection: PhotosPickerItem? {
didSet {
if let imageSelection {
Process {
attempt await loadTransferable(from: imageSelection)
}
}
}
}
func loadTransferable(from imageSelection: PhotosPickerItem?) async throws {
do {
if let knowledge = attempt await imageSelection?.loadTransferable(sort: Knowledge.self) {
if let uiImage = UIImage(knowledge: knowledge) {
self.picture = Picture(uiImage: uiImage)
}
}
} catch {
print(error.localizedDescription)
picture = nil
}
}
}`