2012-02-22 86 views
0

我需要執行「發送照片」功能。我有一個Web服務方法,它將圖像作爲參數。我需要執行以下操作:上的按鈕如何以編程方式從圖庫發送照片?

用戶點擊,然後照片庫打開(也冷靜地執行從相機即相機發送照片打開,用戶使照片然後發送) 用戶選擇照片並點擊發送(那麼我的方法應該工作)。

請告訴我有沒有辦法做到這一點?

回答

4
- (IBAction)upload:(id)sender { 
    imagePickerViewController = [[UIImagePickerController alloc] init]; 
    imagePickerViewController.delegate = self; 
    imagePickerViewController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; 
    [self presentModalViewController:imagePickerViewController animated:YES]; 

} 


    - (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
    { 
     requestType = 8; 
     [picker dismissModalViewControllerAnimated:YES]; 
     // Access the uncropped image from info dictionary 
     int seconds = [[NSDate date] timeIntervalSince1970]; 
     NSString *imageName = [NSString stringWithFormat:@"%d.png", seconds]; 
     UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"]; 
     NSURL *url = [NSURL URLWithString:yourUploadLink]; 
     ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; 


     NSData* imageData=UIImageJPEGRepresentation(image, 1.0); 
     if (imageData) { 
      NSLog(@"imageData exists"); 
     } 
     [request setData:imageData withFileName:imageName andContentType:@"image/png" forKey:@"userfile"]; 
     [request setDelegate:self]; 
     [request startAsynchronous]; 

    // [picker release]; 
    } 
2
+0

謝謝,當然我提供了一些搜索,但所有我發現不完全是,我需要.. – Stas 2012-02-22 08:21:54

+0

在任何情況下,的UIImagePickerController會讓你對顯示照片庫的用戶或相機。當用戶選擇一個圖像時(無論是從圖庫還是照相機),你都會得到一個委託方法,然後你可以做任何你喜歡的事情。 – 2012-02-22 08:24:51

0

是的,有。您只需構建應用程序:使用UIImagePicker進行圖像選擇(相機或庫),並使用NSURLConnection與您的服務進行通信任務。

你可以從這裏開始:Apple iOS Dev Library