2011-01-22 2 views
6

누군가가 어떻게 ASIHTTPRequest 객체를 사용하여 Objective-c에서 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

Heres는 또한 주어진 최대 크기

//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

멋진 대답에 이미지를 압축하려고 시도하는 ASIFormRequest를 사용하는 예. 나는 C#으로 작업하고있는 프로젝트에 북마크되어있다. –

+0

위대한 대답. while 루프 디자인은 정말 유용합니다. –

+0

'setData'보다'addData'를 사용해야하는 이유는 무엇입니까? – Tim

관련 문제