将图像上传到AWS耗时太长 - 压缩?

时间:2016-05-15 01:03:50

标签: ios amazon-web-services amazon-s3

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

Compress images to reduce file size

1 个答案:

答案 0 :(得分:3)

  • 使用此方法压缩图像
  • 将此方法称为:

首先设置图像尺寸

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;
}