2011-07-04 128 views
0

我想一個 UIImage 上傳到服務器。上傳圖片到服務器

我不知道如何將它添加到我的崗位到服務器的問題。 直到現在我發送的發佈與硫:

NSString *reqURL = [NSString stringWithFormat:@"url"]; 
    reqURL = [reqURL stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]; 
    NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:reqURL]]; 
    NSURLResponse *resp = nil; 
    NSError *err = nil; 
    NSData *response = [NSURLConnection sendSynchronousRequest: theRequest returningResponse: &resp error: &err]; 

有任何不同的方式進行圖像呢?

回答

0

使用ASIHttpRequest

-(void)uploadImage{ 
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; 

    // Upload a file on disk 
    [request setFile:@"/Users/ben/Desktop/ben.jpg" withFileName:@"myphoto.jpg" andContentType:@"image/jpeg" 
forKey:@"photo"]; 

    // Upload an NSData instance 
    [request setData:UIImageJPEGRepresentation(myUIImage) withFileName:@"myphoto.jpg" andContentType:@"image/jpeg" forKey:@"photo"]; 

    [request setDelegate:self]; 
    [request setDidFinishSelector:@selector(uploadRequestFinished:)]; 
    [request setDidFailSelector:@selector(uploadRequestFailed:)]; 

    [request startAsynchronous]; 
} 
0

This answer指向this wrapper framework這應該很容易做到你想要的而不處理HTTP頭字符串等等。

基本上,您將使用UIImagePNGRepresentation()之類的函數將您的UIImage變爲NSData對象,然後通過HTTP將其上載。