gpt4 book ai didi

ios - 将图像上传到 AWS 压缩时间过长?

转载 作者:行者123 更新时间:2023-11-28 19:37:40 25 4
gpt4 key购买 nike

所以我将我的 S3 存储桶设置为美国标准,但图像上传到存储桶大约需要 10-15 秒。现在想想,我猜是因为我没有压缩图像,而是将相机拍摄的图像存储到文件路径中,然后上传它。我想知道为了压缩图像我会使用 UIImageJPEGRepresentation,将 NSData 写入文件,然后将该文件上传到 S3 吗?或者有更好/不同的方式吗?或者,如果上传速度慢不是因为压缩,它是否恰好是为存储桶选择的区域?我不完全确定压缩图像是否会加快上传时间,但这可能是造成延迟的一个重要原因

Compress images to reduce file size

最佳答案

  • 使用此方法压缩图片
  • 像这样调用这个方法:

首先设置图片大小

CGSize newSize = CGSizeMake(200, 200);

* UIImage *profileImage = [AppManager resizeImageToSize:newSize image:croppedImage];

#pragma mark - resize Image ToSize
+ (UIImage *)resizeImageToSize:(CGSize)targetSize image: (UIImage*)captureImage
{
UIImage *sourceImage = captureImage;
UIImage *newImage = nil;

CGSize imageSize = sourceImage.size;
CGFloat width = imageSize.width;
CGFloat height = imageSize.height;

CGFloat targetWidth = targetSize.width;
CGFloat targetHeight = targetSize.height;

CGFloat scaleFactor = 0.0;
CGFloat scaledWidth = targetWidth;
CGFloat scaledHeight = targetHeight;

CGPoint thumbnailPoint = CGPointMake(0.0,0.0);

if (CGSizeEqualToSize(imageSize, targetSize) == NO) {

CGFloat widthFactor = targetWidth / width;
CGFloat heightFactor = targetHeight / height;

if (widthFactor < heightFactor)
scaleFactor = widthFactor;
else
scaleFactor = heightFactor;

scaledWidth = width * scaleFactor;
scaledHeight = height * scaleFactor;

// make image center aligned
if (widthFactor < heightFactor)
{
thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
}
else if (widthFactor > heightFactor)
{
thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
}
}

UIGraphicsBeginImageContext(targetSize);
CGRect thumbnailRect = CGRectZero;
thumbnailRect.origin = thumbnailPoint;
thumbnailRect.size.width = scaledWidth;
thumbnailRect.size.height = scaledHeight;

[sourceImage drawInRect:thumbnailRect];
newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

if(newImage == nil)
CCLogs(@"could not scale image");

return newImage;
}

关于ios - 将图像上传到 AWS 压缩时间过长?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37233428/

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