如何在上传到服务器之前在iPhone OS SDK上压缩/调整图像大小?
我正在使用下面的代码在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"];
在模拟器中运行时,代码工作正常,并从模拟器的照片库上载文件,因为我在快速以太网连接上。 但是,当select使用iPhone拍摄的图像时,相同的代码会在iPhone上超时。 所以,我试图从networking上保存一个小图片,并尝试上传,这工作。
这导致我相信iPhone所拍摄的大图像会超越3Gnetworking的速度。 有没有办法在发送之前从iPhone压缩/调整图像大小?
谢谢!
你应该可以做一个像这样的小图像
UIImage *small = [UIImage imageWithCGImage:original.CGImage scale:0.25 orientation:original.imageOrientation];
(对于四分之一大小的图像),然后将较小的图像转换为PNG或任何您需要的格式。
这段代码将调整图片大小:
UIGraphicsBeginImageContext(newSize); [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)]; UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext();
variablesnewSize
是一个CGSize
,可以这样定义:
CGSize newSize = CGSizeMake(100.0f, 100.0f);
一个独立的解决scheme:
- (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的补充,这也许是最快最好的方法。
要在iphonedevelopertips.com上链接到John Muchow的post,在UIImage中添加一个类别是非常快速的方法。 只是打电话
UIImage *_image = [[[UIImage alloc] initWithData:SOME_NSDATA] scaleToSize:CGSizeMake(640.0,480.0)];
返回您的NSDATA(可能是一个在线图像)的640×480表示图像,无需更多的代码行。
马特Gemmell的MGImageUtilities是非常好的,有效地调整和一些努力减less的方法。
在这个代码中0.5意味着50%…
UIImage *original = image; UIImage *compressedImage = UIImageJPEGRepresentation(original, 0.5f);
使用这个简单的方法NSData *data = UIImageJPEGRepresentation(chosenImage, 0.2f);
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 }
#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; }
Swift 2.0版本的Jagandeep Singh方法,但需要将数据转换为图像,因为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&After —图像大小(字节):23KB
-(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)];
它返回图像这个图像,你可以显示任何地方………..