2016-05-15 20 views
0

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

Compress images to reduce file size

回答

1
  • 使用此方法来压缩图像
  • 为延迟一个巨大的原因

    调用此方法,如:

    第一盘映像大小

    CGSize新尺寸= CGSizeMake(200,200); UIImage * profileImage = [AppManager resizeImageToSize:newSize image:croppedImage];我们可以看到,

编译标记 - 调整图像ToSize

  • (UIImage的)resizeImageToSize:(CGSize)的targetSize图像:(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);

    如果(CGSizeEqualToSize(IMAGESIZE,的targetSize)==否){

    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;

}

+0

谢谢shiju86所以是缩放/压缩是问题,现在是100倍倍的速度,我跟着这个链接http://stackoverflow.com/questions/17018617/how-to-resize- an-image-in-ios – abcf

+0

嘿,太棒了! :) –