0

我的应用程序允许用户在相机中拍照/或从他的照片库中选择。

我希望发布的图像的分辨率为 650X550

但是我看到:

此代码之后的大小为 320X480

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
    {
        chosenImage = image;
        NSLog(@"The widht is %f",image.size.width);
        NSLog(@"The height is %f",image.size.height);
    }

当我像这样提交图像时 - 我在服务器端以 1024X480 检索它

 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://myurl/upload/"] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60];
    [request setHTTPMethod:@"POST"];
    NSMutableData *body = [NSMutableData data];
    NSString *boundary = @"---------------------------14737809831466499882746641449";
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
    [request addValue:contentType forHTTPHeaderField:@"Content-Type"];
    NSData *imageData = UIImageJPEGRepresentation(appDelegate.chosenImage, 0.5);
    // file
    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[@"Content-Disposition: form-data; name=\"file\"; filename=\"iphone.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:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    // close form
    [body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    // set request body
    [request setHTTPBody:body];

我想将它作为 650X550(我在 objcetive-c 中声明的大小)检索到我在服务器端的代码中

当我尝试使用常规 HTML 页面提交到我的后端代码时 - 它发送图像的原始大小而不是 1024X480。

如何以原始大小检索图像?

4

1 回答 1

1

您对从选择器获得的尺寸没有影响——尽管它应该大得多

您可以在上传之前手动缩放它

#import <UIKit/UIKit.h>
@interface UIImage (Resize)
- (UIImage*)scaleToSize:(CGSize)size;
@end

@implementation UIImage (Resizing)

- (UIImage*)scaleToSize:(CGSize)size {
    UIGraphicsBeginImageContext(size);

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextTranslateCTM(context, 0.0, size.height);
    CGContextScaleCTM(context, 1.0, -1.0);

    CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, size.width, size.height), self.CGImage);

    UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return scaledImage;
}
@end

演示:

对象端:

UIImage *demo = [UIImage imageNamed:@"demo.jpg"];
UIImage *scaledImage = [demo scaleToSize:CGSizeMake(650, 550)];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://www.pich.info/test.php"] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60];
[request setHTTPMethod:@"POST"];
NSMutableData *body = [NSMutableData data];
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request addValue:contentType forHTTPHeaderField:@"Content-Type"];
NSData *imageData = UIImageJPEGRepresentation(scaledImage, 0.5);
// file
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Disposition: form-data; name=\"file\"; filename=\"iphone.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:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
// close form
[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// set request body
[request setHTTPBody:body];

[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *r, NSData *d, NSError *e) {
    NSLog(@"response: %@", [[NSString alloc] initWithData:d encoding:NSUTF8StringEncoding]);
}];

<?php 
//copy file IF a file was uploaded

$imgFile = $_FILES['file']['tmp_name'];
if(!file_exists($imgFile))
{
    var_dump($_FILES);
    die("no file uploaded");
}

print " File: $imgFile\n";

$im = imagecreatefromjpeg($imgFile);
$imgWidth = imagesx($im);
$imgHeight = imagesy($im);


print " Width: $imgWidth\n";
print "Height: $imgHeight\n";
print "Format: red:green:blue\n";
于 2012-12-10T09:31:01.807 回答