我有一个程序将通过我的iPad应用程序拍摄的图像发送到数据库(下面的代码)。有没有办法在张贴之前缩小图像尺寸?
UIImage *image = [photos objectAtIndex:i];
NSData *imageData = UIImageJPEGRepresentation(image, 10);
// setting up the request object
NSMutableURLRequest *photoRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:photoPostString] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
[photoRequest setHTTPMethod:@"POST"];
NSMutableURLRequest *photoRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:photoPostString] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
[photoRequest setHTTPMethod:@"POST"];
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
photoRequest addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"image\"; filename=\"1234.jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[photoRequest setHTTPBody:body];
答案 0 :(得分:2)
您可以在上传之前调整UIImage的大小。
+ (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {
//UIGraphicsBeginImageContext(newSize);
// In next line, pass 0.0 to use the current device's pixel scaling factor (and thus account for Retina resolution).
// Pass 1.0 to force exact pixel size.
UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
或者一些助手类:
答案 1 :(得分:2)
https://github.com/AliSoftware/UIImage-Resize
将此类别(" UIImage + Resize.h"&" UIImage + Resize.m")添加到您的项目中。
UIImage *newImage = [existingImage resizedImageToSize:CGSizeMake(128, 128)];