在上传到服务器之前,如何在 iOS 上压缩/调整图像大小?

我目前正在上传一个图像到一个服务器上,使用 iOS 上的 Imgur,代码如下:

NSData* imageData = UIImagePNGRepresentation(image);
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* fullPathToFile = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"SBTempImage.png"];
[imageData writeToFile:fullPathToFile atomically:NO];


[uploadRequest setFile:fullPathToFile forKey:@"image"];

当在模拟器中运行并从模拟器的照片库上传文件时,代码工作得很好,因为我使用的是快速以太网连接。但是,同样的代码在 iPhone 上选择用 iPhone 拍摄的图像时会超时。所以,我试着从网上保存一个小图片,然后尝试上传,结果成功了。

这让我相信,iPhone 拍摄的大图片正在有点慢的3G 网络上超时。有没有办法在发送之前压缩或调整 iPhone 上的图片大小?

98443 次浏览

您应该能够通过执行以下操作来制作一个较小的图像

UIImage *small = [UIImage imageWithCGImage:original.CGImage scale:0.25 orientation:original.imageOrientation];

(对于四分之一大小的图像)然后转换较小的图像到一个 PNG 或任何你需要的格式。

Matt Gemmell 的 MGImageUtitility是非常好的,调整大小有效和一些努力减少的方法。

这段代码将调整图像的大小:

UIGraphicsBeginImageContext(newSize);
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

变量 newSizeCGSize,可以这样定义:

CGSize newSize = CGSizeMake(100.0f, 100.0f);

作为@Tuan Nguyen 的补充,这可能是最快也是最优雅的方式。

要链接到 约翰 · 穆周在 iphonedevelopertips.com 的职位,向 UIImage 添加类别是一种非常方便的快速扩展方式。 只是打个电话

    UIImage *_image = [[[UIImage alloc] initWithData:SOME_NSDATA] scaleToSize:CGSizeMake(640.0,480.0)];

返回您的 NSDATA (可以是在线图像)的640x480表示图像,而不需要更多的代码行。

-(UIImage *) resizeImage:(UIImage *)orginalImage resizeSize:(CGSize)size
{
CGFloat actualHeight = orginalImage.size.height;
CGFloat actualWidth = orginalImage.size.width;
//  if(actualWidth <= size.width && actualHeight<=size.height)
//  {
//      return orginalImage;
//  }
float oldRatio = actualWidth/actualHeight;
float newRatio = size.width/size.height;
if(oldRatio < newRatio)
{
oldRatio = size.height/actualHeight;
actualWidth = oldRatio * actualWidth;
actualHeight = size.height;
}
else
{
oldRatio = size.width/actualWidth;
actualHeight = oldRatio * actualHeight;
actualWidth = size.width;
}


CGRect rect = CGRectMake(0.0,0.0,actualWidth,actualHeight);
UIGraphicsBeginImageContext(rect.size);
[orginalImage drawInRect:rect];
orginalImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return orginalImage;
}

这是调用

 UIImage *compimage=[appdel resizeImage:imagemain resizeSize:CGSizeMake(40,40)];

它返回图像这个图像你可以显示任何地方... ... ... ..。

一个独立的解决方案:

- (UIImage *)compressForUpload:(UIImage *)original scale:(CGFloat)scale
{
// Calculate new size given scale factor.
CGSize originalSize = original.size;
CGSize newSize = CGSizeMake(originalSize.width * scale, originalSize.height * scale);


// Scale the original image to match the new size.
UIGraphicsBeginImageContext(newSize);
[original drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *compressedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();


return compressedImage;
}

感谢@Tuan Nguyen。

使用这个简单的方法 NSData *data = UIImageJPEGRepresentation(chosenImage, 0.2f);

#import <ImageIO/ImageIO.h>
#import <MobileCoreServices/MobileCoreServices.h>


+ (UIImage *)resizeImage:(UIImage *)image toResolution:(int)resolution {
NSData *imageData = UIImagePNGRepresentation(image);
CGImageSourceRef src = CGImageSourceCreateWithData((__bridge CFDataRef)imageData, NULL);
CFDictionaryRef options = (__bridge CFDictionaryRef) @{
(id) kCGImageSourceCreateThumbnailWithTransform : @YES,
(id) kCGImageSourceCreateThumbnailFromImageAlways : @YES,
(id) kCGImageSourceThumbnailMaxPixelSize : @(resolution)
};
CGImageRef thumbnail = CGImageSourceCreateThumbnailAtIndex(src, 0, options);
CFRelease(src);
UIImage *img = [[UIImage alloc]initWithCGImage:thumbnail];
return img;
}

Zorayr 函数的快速实现(有一点改变,包括实际单位的高度或宽度约束,而不是比例) :

class func compressForUpload(original:UIImage, withHeightLimit heightLimit:CGFloat, andWidthLimit widthLimit:CGFloat)->UIImage{


let originalSize = original.size
var newSize = originalSize


if originalSize.width > widthLimit && originalSize.width > originalSize.height {


newSize.width = widthLimit
newSize.height = originalSize.height*(widthLimit/originalSize.width)
}else if originalSize.height > heightLimit && originalSize.height > originalSize.width {


newSize.height = heightLimit
newSize.width = originalSize.width*(heightLimit/originalSize.height)
}


// Scale the original image to match the new size.
UIGraphicsBeginImageContext(newSize)
original.drawInRect(CGRectMake(0, 0, newSize.width, newSize.height))
let compressedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();


return compressedImage
}

在这个代码里0.5意味着50% ..。

UIImage *original = image;
UIImage *compressedImage = UIImageJPEGRepresentation(original, 0.5f);

Jagandeep Singh 方法的 Swift 2.0版本,但需要将数据转换为图像,因为 NSData?未自动转换 UIImage。

let orginalImage:UIImage = image


let compressedData = UIImageJPEGRepresentation(orginalImage, 0.5)
let compressedImage = UIImage(data: compressedData!)
NsData *data=UiImageJPEGRepresentation(Img.image,0.2f);
UIImage *image = [UIImage imageNamed:@"image.png"];
NSData *imgData1 = UIImageJPEGRepresentation(image, 1);
NSLog(@"Original --- Size of Image(bytes):%d",[imgData1 length]);


NSData *imgData2 = UIImageJPEGRepresentation(image, 0.5);
NSLog(@"After --- Size of Image(bytes):%d",[imgData2 length]);
image = [UIImage imageWithData:imgData2];
imgTest.image = image;

尝试通过缩放因子转换 JPG。这里我使用0.5 就我而言: 原始——-图像大小(字节) : 85KB; 之后——-图像大小(字节) : 23KB