2011-09-30 141 views
12

我正在處理與我在此處找到的示例代碼幾乎相同的應用程序。但是我想要做的方法與示例代碼不同。
鏈接:PhotoLocations如何從照片庫加載照片並將其存儲到應用程序項目中?

第一個屏幕將有一個ImageView和2個按鈕(選擇照片,確認)。 當點擊「選擇照片」按鈕時,它將導航到另一個屏幕,從iPad的照片庫中檢索照片。當選擇照片時,它將關閉當前屏幕並返回到在ImageView上顯示所選照片的​​第一個屏幕。

點擊'確認'按鈕後,照片將被存儲到我的應用程序的項目中(例如/resources/images/photo.jpg)。

我可以知道我該怎麼做?

+0

在iPhone,你必須使用的UIImagePickerController來檢索圖庫圖片,您可以通過它保存在NSDocumentsDirectory – booleanBoy

+0

是謝謝你讓他們到烏爾應用程序,但我希望有一些示例代碼,以便我可以參考。 – Lloydworth

+2

谷歌「UIImagePickerController參考」。轉到Apple的網站。 – Akshay

回答

32

這將帶你到圖片庫,你可以選擇圖像。

UIImagePickerController *imagePickerController = [[UIImagePickerController alloc]init]; 
imagePickerController.delegate = self; 
imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; 
[self presentViewController:imagePickerController animated:YES completion:nil]; 

這將幫助你選擇圖像

- (void)imagePickerController:(UIImagePickerController *)picker 
    didFinishPickingImage:(UIImage *)image 
       editingInfo:(NSDictionary *)editingInfo 
{ 
    // Dismiss the image selection, hide the picker and 

    //show the image view with the picked image 

    [picker dismissViewControllerAnimated:YES completion:nil]; 
    //UIImage *newImage = image; 
} 

然後你就可以保存此圖片的文件目錄...

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,  NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:@"savedImage.png"]; 
UIImage *image = imageView.image; // imageView is my image from camera 
NSData *imageData = UIImagePNGRepresentation(image); 
[imageData writeToFile:savedImagePath atomically:NO]; 

對於點擊圖片自己使用這個

- (IBAction) takePhoto:(id) sender 
{ 
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init]; 

    imagePickerController.delegate = self; 
    imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera; 

    [self presentModalViewController:imagePickerController animated:YES]; 
} 
+0

我可以知道如何將圖像從ImageView存儲到文檔目錄? – Lloydworth

+2

剛剛編輯我上面的答案... –

+0

我只是想到了一個額外的功能。在第一個屏幕中,我會將「選擇照片」按鈕更改爲「從照片庫瀏覽」,並添加一個新按鈕「拍攝新照片」。通過點擊該按鈕,我的應用程序將啓動相機,一旦用戶拍攝照片,它將返回到第一個屏幕,顯示ImageView上的圖片,該圖片也可以保存到我的文檔目錄中。 – Lloydworth

3
UIImagePickerController *cardPicker = [[UIImagePickerController alloc]init]; 
cardPicker.allowsEditing=YES; 
cardPicker.delegate=self; 
cardPicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum; 
[self presentModalViewController:appDelegate.cardPicker animated:YES]; 
[cardPicker release]; 

而且此委託方法將返回U選擇將圖像從專輯

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editInfo 
{ 
    [[picker parentViewController] dismissModalViewControllerAnimated:YES]; 
    noImage.image=img; 
} 
+0

其中noImage是UIImageView,我在其中顯示從相冊中選取的圖像... – booleanBoy

+0

我可以知道如何將noImage存儲到文檔目錄嗎? – Lloydworth

0

- (IBAction)UploadPhoto:(id)sender { 
 
    
 
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Please Select Your Option"delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Camera", @"Gallery",nil]; 
 
    
 
    [actionSheet showInView:self.view]; 
 
    
 
    UITableViewCell *clickedCell = (UITableViewCell *)[[sender superview] superview]; 
 
    
 
    NSIndexPath *clickedButtonPath = [jobstable indexPathForCell:clickedCell]; 
 
    
 
    isSectionIndex = clickedButtonPath.section; 
 
    isRowIndex  = clickedButtonPath.row; 
 
    
 
} 
 

 

 
-(void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex{ 
 
    
 
    
 
    NSLog(@"From didDismissWithButtonIndex - Selected Option: %@", [actionSheet buttonTitleAtIndex:buttonIndex]); 
 
    
 
    NSString*image=[actionSheet buttonTitleAtIndex:buttonIndex]; 
 
    
 
    if ([[actionSheet buttonTitleAtIndex:buttonIndex] isEqualToString:@"Camera"]) { 
 
     
 
     if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) 
 
     { 
 
      UIAlertView* alert = [[UIAlertView alloc] initWithTitle:nil message:@"Device Camera Is Not Working" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil]; 
 
      [alert show]; 
 
      return; 
 
     } 
 
     else{ 
 
      
 
      UIImagePickerController *picker = [[UIImagePickerController alloc] init]; 
 
      picker.delegate = self; 
 
      picker.allowsEditing = YES; 
 
      picker.sourceType = UIImagePickerControllerSourceTypeCamera; 
 
      
 
      [self presentViewController:picker animated:YES completion:NULL]; 
 
      
 
     } 
 
    } 
 
    
 
    else if ([[actionSheet buttonTitleAtIndex:buttonIndex] isEqualToString:@"Gallery"]){ 
 
     
 
     
 
     UIImagePickerController *pickerView = [[UIImagePickerController alloc] init]; 
 
     pickerView.allowsEditing = YES; 
 
     pickerView.delegate = self; 
 
     [pickerView setSourceType:UIImagePickerControllerSourceTypeSavedPhotosAlbum]; 
 
     [self presentViewController:pickerView animated:YES completion:nil]; 
 
     
 
    } 
 
    
 
} 
 

 
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{ 
 
UIImage* orginalImage = [info objectForKey:UIImagePickerControllerOriginalImage]; 
 
     
 
     
 
     
 
     NSIndexPath *indexPath = [NSIndexPath indexPathForRow:isRowIndex inSection:isSectionIndex]; 
 
     
 
     UITableViewCell *cell = [jobstable cellForRowAtIndexPath:indexPath]; 
 
     
 
     UIImageView *tableIMAGE=(UIImageView *)[cell.contentView viewWithTag:19]; 
 
     
 
     tableIMAGE.image=orginalImage; 
 
     
 
    answersARRAY[indexPath.row] = [NSString stringWithFormat:@"-1,%@,%@,",answersARRAY[indexPath.row],imageStris]; 
 

 
    
 
     
 
     [self dismissViewControllerAnimated:YES completion:nil]; 
 
     
 
     
 
    } 
 
    
 
    - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker { 
 
     
 
     [picker dismissViewControllerAnimated:YES completion:NULL]; 
 
    
 
    }

+0

這是對已經接受答案的舊問題的新答案。你的回答提供了什麼,接受的答案不是?此外,添加關於此代碼如何工作的解釋將有助於未來的訪問者。 – JAL

相關問題