abcf
abcf

Reputation: 695

Uploading images to AWS taking too long -compression?

So I set my S3 bucket to US Standard, but it takes about 10-15 seconds for images to upload to the bucket. Now that I think about it, I'm guessing it's because I'm not compressing the images, instead I'm just storing the image taken with the camera into a filepath and then uploading it. I was wondering in order to compress the image would I use UIImageJPEGRepresentation, write the NSData to the file, and then upload that file to S3? Or is there a better/different way? Or if the slow uploading isn't because of compression, would it happen to be the region chosen for the bucket? I'm not entirely sure if compressing the images will speed up the time it takes to upload, it could be a huge reason for the latency though

Compress images to reduce file size

Upvotes: 0

Views: 1166

Answers (1)

shiju86.v
shiju86.v

Reputation: 667

  • Use this method to compress image
  • Call this method like:

First Set your image 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;
}

Upvotes: 3

Related Questions