首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >snapchat如何调整图像大小?

snapchat如何调整图像大小?
EN

Stack Overflow用户
提问于 2017-01-19 15:33:21
回答 1查看 2.4K关注 0票数 0

SnapChat如何在保持良好质量的情况下将图像调整为如此小的文件大小?我的图像保存在90KB左右的后置摄像头和45kb的前置摄像头。我在网上读到,发送一个快照大约是15kb。如何更好地优化镜像?

下面是我的代码,它可以拍摄照片,调整大小,并对其进行优化,以便发布:

代码语言:javascript
复制
// 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()
            }
        })
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-01-19 17:11:28

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

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

代码语言:javascript
复制
- (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];
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41736196

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档