我目前正在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的图像大小?
答案 0 :(得分:201)
此代码段会调整图片大小:
UIGraphicsBeginImageContext(newSize);
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
变量newSize
是CGSize
,可以这样定义:
CGSize newSize = CGSizeMake(100.0f, 100.0f);
答案 1 :(得分:38)
一个独立的解决方案:
- (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。
答案 2 :(得分:16)
为了补充@Tuan Nguyen,这可能是最快,最优雅的方式。
要链接到John Muchow's post at iphonedevelopertips.com,向UIImage添加类别是一种非常方便的扩展方式。 只需致电
UIImage *_image = [[[UIImage alloc] initWithData:SOME_NSDATA] scaleToSize:CGSizeMake(640.0,480.0)];
返回NSDATA的640x480表示图像(可能是在线图像),不再需要任何代码。
答案 3 :(得分:7)
Matt Gemmell的MGImageUtilities非常好,通过一些努力减少方法有效地调整大小。
答案 4 :(得分:6)
在此代码中,0.5表示50%......
UIImage *original = image;
UIImage *compressedImage = UIImageJPEGRepresentation(original, 0.5f);
答案 5 :(得分:4)
使用这个简单的方法NSData *data = UIImageJPEGRepresentation(chosenImage, 0.2f);
答案 6 :(得分:3)
迅速实施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
}
答案 7 :(得分:0)
#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;
}
答案 8 :(得分:0)
Swift 2.0版本的Jagandeep Singh方法但需要将数据转换为图像,因为NSData?没有自动转换UIImage。
let orginalImage:UIImage = image
let compressedData = UIImageJPEGRepresentation(orginalImage, 0.5)
let compressedImage = UIImage(data: compressedData!)
答案 9 :(得分:0)
NsData *data=UiImageJPEGRepresentation(Img.image,0.2f);
答案 10 :(得分:0)
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&amp; ---图像大小(字节):23KB
答案 11 :(得分:-1)
-(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)];
它返回图像这个图像你可以显示任何...........
答案 12 :(得分:-33)
您可以通过执行类似
的操作来制作较小的图像UIImage *small = [UIImage imageWithCGImage:original.CGImage scale:0.25 orientation:original.imageOrientation];
(对于四分之一尺寸的图像)然后将较小的图像转换为PNG或您需要的任何格式。