Ios 在Xcode中以HTTP POST发送图像之前,请减小图像的大小

Ios 在Xcode中以HTTP POST发送图像之前,请减小图像的大小,ios,http-post,jpeg,Ios,Http Post,Jpeg,我有一个程序,将通过我的iPad应用程序拍摄的图像发送到数据库(代码如下)。有没有办法在图片发布前缩小图片的大小 UIImage *image = [photos objectAtIndex:i]; NSData *imageData = UIImageJPEGRepresentation(image, 10); // setting up the request object NSMutableURLRequest *photoRequest = [NSMutableURLRequest r

我有一个程序,将通过我的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];

您可以在上载之前调整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;
}
或某些助手类:

将此类别(“UIImage+Resize.h”和“UIImage+Resize.m”)添加到项目中

UIImage *newImage = [existingImage resizedImageToSize:CGSizeMake(128, 128)];

谢谢它可以用%来完成吗?这样它就可以同时适用于风景画和肖像画,而不必单独处理宽度和高度?