Snapchat如何调整图片大小?

时间:2017-01-19 07:33:21

标签: ios swift image swift3 compression

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()
            }
        })
    }
}

1 个答案:

答案 0 :(得分: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];
}