2011-06-20 36 views
0

直到現在我送strings到服務器用這種方法:發送的UIImage到服務器

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

,我想知道是否有可能將一個UIImage發送到服務器。

回答

2

如果您的圖片是png,然後使用UIImagePNGRepresentation獲取它的數據在NSData

NSData *data = UIImagePNGRepresentation(myUIImage); 

,如果你的形象是jpgjpeg然後使用UIImageJPEGRepresentation來獲得NSData它的數據。

NSData *data = UIImageJPEGRepresentation(myUIImage); 

入住Documentation

檢查下方,以便張貼發送NSDataUIImage到服務器。

Sending an image data(NSData) to the server

1

你可以試試這個: -

NSData *imagedata=[NSData dataWithData:UIImagePNGRepresentation(self.editedImage)]; 
    NSString *base64string=[imagedata base64EncodedString]; 
    NSString *str = [NSString stringWithFormat:@"%@/uploadBlogData.php",appUrl]; 
    NSURL *url = [NSURL URLWithString:[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; 
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; 
    [request setPostValue:base64string forKey:@"imagedata"]; 
    [request setRequestMethod:@"POST"]; 
    [request setDelegate:self]; 
    [request startSynchronous]; 

     NSLog(@"responseStatusCode %i",[request responseStatusCode]); 
    NSLog(@"responseStatusString %@",[request responseString]); 

這段代碼究竟這樣做,我有我的轉換成圖像NSData的,然後再在基地64字符串編碼。 你也可以這樣做。

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]; 
}