gpt4 book ai didi

ios - snapchat 如何调整图像大小?

转载 作者:行者123 更新时间:2023-11-28 15:55:58 27 4
gpt4 key购买 nike

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

最佳答案

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

幸运的是,您不是第一个遇到此问题的人。 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];
}

关于ios - snapchat 如何调整图像大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41736196/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com