2011-09-10 26 views
0

使用的例子,從iPhone的圖像,例如下面的一個從this website我試圖發送帶有的UIImagePickerController選擇照片。發送使用ASIHTTP和UIImagePicker

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; 
[request addPostValue:@"Ben" forKey:@"names"]; 
[request addPostValue:@"George" forKey:@"names"]; 
[request addFile:@"/Users/ben/Desktop/ben.jpg" forKey:@"photos"]; 
[request addData:imageData withFileName:@"george.jpg" andContentType:@"image/jpeg" forKey:@"photos"]; 

imageURL = [info valueForKey:UIImagePickerControllerReferenceURL]; 
NSLog(@" %@ ", imageURL); 
stringURL = [imageURL absoluteString]; 

我不明白如何連接這兩個代碼塊。我想用這個URL在我的iPhone上找到圖像,然後用ASIHTTP代碼發送它。有任何想法嗎?

回答

1

首先,你需要保存圖像和跟蹤文件名或文件路徑的。在這裏我使用了一段時間來創建獨特的圖像名稱。 photoImage是你想存儲的UIImage。

NSTimeInterval timeInterval = [NSDate timeIntervalSinceReferenceDate]; 
NSString *photoName=[NSString stringWithFormat:@"%lf-Photo.jpeg",timeInterval]; 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
// the path to write file 
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:photoName]; 
NSData * photoImageData = UIImageJPEGRepresentation(photoImage, 1.0); 
[photoImageData writeToFile:appFile atomically:YES]; 

然後你去使用你存儲的文件名搶的文件路徑,或者如果您已經存儲的文件路徑,然後跳到下一位。

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:photoName]; 

然後我們發出HTTP請求在所述文件路徑上發送數據。

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; 
request.requestMethod = @"POST"; 
[request setFile:filePath forKey:@"file1"]; 
[request startSynchronous];