【问题标题】:How does snapchat resize images?snapchat 如何调整图像大小?
【发布时间】:2017-01-19 07:33:21
【问题描述】:

SnapChat 如何将图像调整为如此小的文件大小,同时仍保持良好的质量?我的图像为后置摄像头节省了大约 90KB,为前置摄像头节省了 45kb。我在网上看到发送一个快照大约是 15kb。如何更好地优化图像?

这是我的代码,用于拍照、调整大小和优化以发布:

// Takes a still image of a video, then resizes image while user is deciding whether to post or retake it
@IBAction func takePictureButton(_ sender: Any) {

    if let videoConnection = stillImageOutput?.connection(withMediaType: AVMediaTypeVideo){
        videoConnection.videoOrientation = AVCaptureVideoOrientation.portrait
        stillImageOutput?.captureStillImageAsynchronously(from: videoConnection, completionHandler: {
            (sampleBuffer, error) in

            if sampleBuffer != nil {

                self.takePicture.isHidden = true
                self.rotate.isHidden = true
                self.back.isHidden = true
                self.save.isHidden = false
                self.retake.isHidden = false

                let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(sampleBuffer)
                let dataProvider = CGDataProvider(data: imageData as! CFData)
                let cgImageRef = CGImage(jpegDataProviderSource: dataProvider!, decode: nil, shouldInterpolate: true, intent: CGColorRenderingIntent.defaultIntent)

                // Handles scaling image while user decides whether or not to post or retake
                // Scale this up for smaller images.. idk why
                let image = UIImage(cgImage: cgImageRef!, scale: 1.0, orientation: UIImageOrientation.right)
                let size = image.size.applying(CGAffineTransform(scaleX: 0.4, y: 0.4))

                UIGraphicsBeginImageContextWithOptions(size, true, 0.0)
                image.draw(in: CGRect(origin: .zero, size: size))

                let scaledImage = UIGraphicsGetImageFromCurrentImageContext()
                UIGraphicsEndImageContext()

                self.imageDataToSend = UIImageJPEGRepresentation(scaledImage!, 0.4)
                self.imageToUpload = image
                self.didTakePhoto = true
                self.tempImageView.image = image
                self.tempImageView.isHidden = false
                self.captureSession?.stopRunning()
            }
        })
    }
}

【问题讨论】:

    标签: ios swift image swift3 compression


    【解决方案1】:

    您必须在调整图像大小/压缩图像时找到完美的平衡。

    幸运的是,您不是第一个遇到此问题的人。 Akshay in Built.io already made some trial and error to reach the perfect balance

    - (UIImage *)compressImage:(UIImage *)image{
        float actualHeight = image.size.height;
        float actualWidth = image.size.width;
        float maxHeight = 600.0;
        float maxWidth = 800.0;
        float imgRatio = actualWidth/actualHeight;
        float maxRatio = maxWidth/maxHeight;
        float compressionQuality = 0.5;//50 percent compression
    
        if (actualHeight > maxHeight || actualWidth > maxWidth) {
            if(imgRatio < maxRatio){
                //adjust width according to maxHeight
                imgRatio = maxHeight / actualHeight;
                actualWidth = imgRatio * actualWidth;
                actualHeight = maxHeight;
            }
            else if(imgRatio > maxRatio){
                //adjust height according to maxWidth
                imgRatio = maxWidth / actualWidth;
                actualHeight = imgRatio * actualHeight;
                actualWidth = maxWidth;
            }else{
                actualHeight = maxHeight;
                actualWidth = maxWidth;
            }
        }
    
        CGRect rect = CGRectMake(0.0, 0.0, actualWidth, actualHeight);
        UIGraphicsBeginImageContext(rect.size);
        [image drawInRect:rect];
        UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
        NSData *imageData = UIImageJPEGRepresentation(img, compressionQuality);
        UIGraphicsEndImageContext();
    
        return [UIImage imageWithData:imageData];
    }
    

    【讨论】:

    • 你知道 Swift 3 中的版本吗?如果不是这样很好,我会把它转换过来。
    • 这个要点有一些东西,但我不知道它是否正确。 gist.github.com/akshay1188/…
    猜你喜欢
    • 2020-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-25
    • 2014-06-03
    • 2010-11-20
    • 1970-01-01
    相关资源
    最近更新 更多