2012-12-10 52 views
0

我的應用程序允許用戶在相機中拍攝照片/或從他的照片庫中進行選擇。執行http post後的UIImage圖像分辨率

我想投稿的圖像是在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 - 我在ob​​jcetive-c聲明的大小 - 我的代碼在服務器端

當我嘗試提交給我的後端代碼與常規的HTML頁面 - 它發送原始大小的圖像,而不是1024X480。

如何實現以原始尺寸檢索圖像?

+1

xcode只是一個IDE,這與ide無關:) –

+0

正確。我修復了它 – Dejell

回答

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 

DEMO之前大得多

可以手動縮放:

objc -side:

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"; 
+0

在前端(xcode)中比在服務器中縮放它不是更好嗎?我不需要它太大 – Dejell

+0

問題一 - 縮放圖像,但我仍然檢索它爲1024X480。我的服務器端是在Java中當我檢索圖像 – Dejell

+0

我很困惑你的單詞檢索的使用,刪除問題2 –