Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
I switched from utilizing UIViewController Picture Picker to Swift’s new native Picture Picker. The picture picker itself works nice however importing to firebase doesn’t.
I need to compress the picture to not refill my Firebase storage too quick. With the previous 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: Didn't add picture with error: (error.localizedDescription)") return }
however now I am unable to 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 methods 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
swap 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?, kind: UploadType, completion: @escaping(String) -> Void) {
let ref = kind.filePath
guard let imageData = Picture.jpegData(compressionQuality: 0.5) else { return }
ref.putData(imageData, metadata: nil) { _, error in
if let error = error {
print("DEBUG: Didn't 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 {
Activity {
attempt await loadTransferable(from: imageSelection)
}
}
}
}
func loadTransferable(from imageSelection: PhotosPickerItem?) async throws {
do {
if let information = attempt await imageSelection?.loadTransferable(kind: Information.self) {
if let uiImage = UIImage(information: information) {
self.picture = Picture(uiImage: uiImage)
}
}
} catch {
print(error.localizedDescription)
picture = nil
}
}
}`