【问题标题】:Uploading images to AWS taking too long -compression?将图像上传到 AWS 需要太长时间 - 压缩?
【发布时间】:2016-09-11 00:48:11
【问题描述】:
所以我将我的 S3 存储桶设置为美国标准,但图像上传到存储桶大约需要 10-15 秒。现在我想起来了,我猜这是因为我没有压缩图像,而是将相机拍摄的图像存储到文件路径中,然后上传。我想知道为了压缩图像我会使用 UIImageJPEGRepresentation,将 NSData 写入文件,然后将该文件上传到 S3?还是有更好/不同的方法?或者,如果上传缓慢不是因为压缩,它是否恰好是为存储桶选择的区域?我不完全确定压缩图像是否会加快上传时间,但这可能是延迟的一个重要原因
Compress images to reduce file size
【问题讨论】:
标签:
ios
amazon-web-services
amazon-s3
【解决方案1】:
首先设置你的图片尺寸
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;
}