Friday, February 3, 2023
HomeiOS Developmentios - How do you compress chosen picture to firebase with Swift's...

ios – How do you compress chosen picture to firebase with Swift’s PhotoPicker?


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 wish 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: Didn't 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 how you can 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: 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 {
                strive await loadTransferable(from: imageSelection)
            }
        }
    }
}

func loadTransferable(from imageSelection: PhotosPickerItem?) async throws {
    do {
        if let information = strive await imageSelection?.loadTransferable(sort: Knowledge.self) {
            if let uiImage = UIImage(information: information) {
                self.picture = Picture(uiImage: uiImage)
            }
        }
    } catch {
        print(error.localizedDescription)
        picture = nil
    }
}

}`

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

16 − ten =

Most Popular

Recent Comments