Ios 将图像上载到AWS花费的时间太长-压缩?
因此,我将S3存储桶设置为美国标准,但图像上传到存储桶大约需要10-15秒。现在我想起来了,我猜这是因为我没有压缩图像,而是将相机拍摄的图像存储到一个文件路径中,然后上传。我想知道,为了压缩图像,我会使用UIImageJPEG表示,将NSData写入文件,然后将该文件上载到S3吗?还是有更好/不同的方法?或者,如果上传速度慢不是因为压缩,那么它会恰好是为bucket选择的区域吗?我不完全确定压缩图像是否会加快上传时间,但这可能是造成延迟的一个重要原因Ios 将图像上载到AWS花费的时间太长-压缩?,ios,amazon-web-services,amazon-s3,Ios,Amazon Web Services,Amazon S3,因此,我将S3存储桶设置为美国标准,但图像上传到存储桶大约需要10-15秒。现在我想起来了,我猜这是因为我没有压缩图像,而是将相机拍摄的图像存储到一个文件路径中,然后上传。我想知道,为了压缩图像,我会使用UIImageJPEG表示,将NSData写入文件,然后将该文件上载到S3吗?还是有更好/不同的方法?或者,如果上传速度慢不是因为压缩,那么它会恰好是为bucket选择的区域吗?我不完全确定压缩图像是否会加快上传时间,但这可能是造成延迟的一个重要原因 使用此方法压缩图像 调用此方法,如下所示:
- 使用此方法压缩图像
- 调用此方法,如下所示:
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;
}
cgsizenewsize=CGSizeMake(200200);
*UIImage*profileImage=[AppManager resizeImageToSize:newSize image:croppedImage];
#pragma标记-将图像调整为大小
+(UIImage*)resizeImageToSize:(CGSize)targetSize图像:(UIImage*)captureImage
{
UIImage*sourceImage=captureImage;
UIImage*newImage=nil;
CGSize imageSize=sourceImage.size;
CGFloat width=imageSize.width;
CGFloat height=图像大小.height;
CGFloat targetWidth=targetSize.width;
CGFloat targetHeight=targetSize.height;
CGFloat scaleFactor=0.0;
CGFloat scaledWidth=targetWidth;
CGFloat缩放高度=目标高度;
CGPoint thumbnailPoint=CGPointMake(0.0,0.0);
if(CGSizeEqualToSize(imageSize,targetSize)==否){
CGFloat宽度系数=目标宽度/宽度;
CGFloat高度系数=目标高度/高度;
if(宽度系数<高度系数)
scaleFactor=宽度因子;
其他的
比例因子=高度因子;
scaledWidth=宽度*缩放因子;
缩放高度=高度*缩放因子;
//使图像中心对齐
if(宽度系数<高度系数)
{
thumbnailPoint.y=(targetLight-缩放高度)*0.5;
}
否则如果(宽度因子>高度因子)
{
thumbnailPoint.x=(targetWidth-缩放宽度)*0.5;
}
}
UIGraphicsBeginImageContext(targetSize);
CGRect thumbnailRect=CGRectZero;
thumbnailRect.origin=thumbnailPoint;
thumbnailRect.size.width=缩放宽度;
thumbnailRect.size.height=缩放高度;
[sourceImage drawInRect:thumbnailRect];
newImage=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsSendImageContext();
if(newImage==nil)
CCLogs(@“无法缩放图像”);
返回新图像;
}
- 使用此方法压缩图像
- 调用此方法,如下所示:
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;
}
cgsizenewsize=CGSizeMake(200200);
*UIImage*profileImage=[AppManager resizeImageToSize:newSize image:croppedImage];
#pragma标记-将图像调整为大小
+(UIImage*)resizeImageToSize:(CGSize)targetSize图像:(UIImage*)captureImage
{
UIImage*sourceImage=captureImage;
UIImage*newImage=nil;
CGSize imageSize=sourceImage.size;
CGFloat width=imageSize.width;
CGFloat height=图像大小.height;
CGFloat targetWidth=targetSize.width;
CGFloat targetHeight=targetSize.height;
CGFloat scaleFactor=0.0;
CGFloat scaledWidth=targetWidth;
CGFloat缩放高度=目标高度;
CGPoint thumbnailPoint=CGPointMake(0.0,0.0);
if(CGSizeEqualToSize(imageSize,targetSize)==否){
CGFloat宽度系数=目标宽度/宽度;
CGFloat高度系数=目标高度/高度;
if(宽度系数<高度系数)
scaleFactor=宽度因子;
其他的
比例因子=高度因子;
scaledWidth=宽度*缩放因子;
缩放高度=高度*缩放因子;
//使图像中心对齐
if(宽度系数<高度系数)
{
thumbnailPoint.y=(targetLight-缩放高度)*0.5;
}
否则如果(宽度因子>高度因子)
{
thumbnailPoint.x=(targetWidth-缩放宽度)*0.5;
}
}
UIGraphicsBeginImageContext(targetSize);
CGRect thumbnailRect=CGRectZero;
thumbnailRect.origin=thumbnailPoint;
thumbnailRect.size.width=缩放宽度;
thumbnailRect.size.height=缩放高度;
[sourceImage drawInRect:thumbnailRect];
newImage=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsSendImageContext();
if(newImage==nil)
CCLogs(@“无法缩放图像”);
返回新图像;
}
感谢shiju86,是的,缩放/压缩是问题所在,现在速度快了100倍,我遵循了此链接感谢shiju86,是的,缩放/压缩是问题所在,现在速度快了100倍,我遵循了此链接