Reduce size of full screen image

135 Views Asked by At

I have an app with a "Snapchat-style Camera" (i.e. you take a picture and then overlay text and emojis). I am currently reducing the resolution to 1080 x 2040 and then converting to jpegRepresentation with a quality of 0.75.

This is still giving me a file size of >2mb which seems high. Any suggestions for how to lower the file size without giving up too much quality? I still want the pictures to look good at full screen size.

Resizing image and then sending to storage

override func post() {
    // convert overlayed text and emojis to image and resize to 1080 x 2040
    guard let image = mediaView.asImage().resized(to: IMAGE_SIZE) else { return }

    //generate unique name for each image
    let imageId = NSUUID().uuidString
    let storageRef = Storage.storage().reference().child("post_images/\(imageId).jpeg")

    storageManager.upload(image: image, to: storageRef) { (err) in
        if let err = err {
            // TODO: Handle Error
            print("Error uploading image: \(err.localizedDescription)")
        } else {
            storageRef.downloadURL { (url, err) in
                guard let url = url else {
                    // TODO: Handle Error
                    print("Error getting image url: \(err?.localizedDescription)")
                    return
                }

                print("SUCCESSFULLY SAVED IMAGE")
            }
        }
    }
}

Storage Manager: Converting image to jpeg and compressing. Then uploading

func upload(image: UIImage, to storageRef: StorageReference, then handler: @escaping UploadHandler) {
    // upload to storage
    if let uploadData = image.jpegData(compressionQuality: 0.75) {
        let metadata = StorageMetadata()
        metadata.contentType = ContentTypeEnum.jpegImage.rawValue // "image/jpeg"

        storageRef.putData(uploadData, metadata: metadata) { (metadata, err) in
            if let err = err {
                handler(err)
            } else {
                handler(nil)
            }
        }
    }
}

Extension to convert UIView to Image

extension UIView {
    func asImage() -> UIImage {
        let renderer = UIGraphicsImageRenderer(bounds: bounds)
        return renderer.image { rendererContext in
            layer.render(in: rendererContext.cgContext)
        }
    }
}

Extension to resize UIImage

extension UIImage {
    func resized(to size: CGSize) -> UIImage? {
        let canvasSize = CGSize(width: size.width, height: size.height)
        UIGraphicsBeginImageContextWithOptions(canvasSize, false, scale)
        defer { UIGraphicsEndImageContext() }
        draw(in: CGRect(origin: .zero, size: canvasSize))
        return UIGraphicsGetImageFromCurrentImageContext()
    }
}
0

There are 0 best solutions below