2011-01-22 83 views
6

有人可以告訴我如何使用Objective-c中的ASIHTTPRequest對象上傳UIImage對象嗎?我需要將其轉換爲NSData對象嗎?ASIHTTP:上傳UIImage?

(這是一個頭像上傳的URL)

E.g. 

UIImage *toUpload = [UIImage imageNamed:@"test.jpg"] 

URL: "http://www.example.com/api/users/avatar/upload?access_token=12345" 
RequestType: PUT 

回答

14

繼承人使用ASIFormRequest也將嘗試將圖像壓縮到給定的最大尺寸

//Compress the image 
CGFloat compression = 0.9f; 
CGFloat maxCompression = 0.1f; 
int maxFileSize = 250*1024; 

NSData *imageData = UIImageJPEGRepresentation(yourImage, compression); 

while ([imageData length] > maxFileSize && compression > maxCompression) 
{ 
    compression -= 0.1; 
    imageData = UIImageJPEGRepresentation(yourImage, compression); 
} 

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:whereYourServerEndPointIs.com]]; 

[request addData:imageData withFileName:@"someFileName.jpeg" andContentType:@"image/jpeg" forKey:@"uploadedImage"]; 
+0

大答案的例子。我有這個書籤爲我正在與C#工作的項目# – 2011-01-23 02:25:36