2011-09-30 4 views
12

Je travaille sur une application qui est presque la même que les exemples de codes que je trouve ici. Mais la méthode que je veux faire est différente des exemples de codes.
Lien: PhotoLocationsComment charger des photos de la galerie de photos et les stocker dans le projet d'application?

Le premier écran comportera un ImageView et 2 boutons (Choisir une photo, Confirmer). Lorsque le bouton «Choisir une photo» est tapé, il va naviguer vers un autre écran qui récupère des photos de la galerie de photos de mon iPad. Lorsqu'une photo est sélectionnée, l'écran en cours est rejeté et le premier écran affichant la photo sélectionnée sur ImageView s'affiche.

Lorsque le bouton 'Confirmer' est appuyé, la photo est enregistrée dans le projet de mon application (par exemple /resources/images/photo.jpg).

Puis-je savoir comment puis-je faire cela?

+0

dans l'iPhone vous devez utiliser UIImagePickerController pour récupérer des images de la galerie et vous pouvez les avoir dans ur application en enregistrant dans NSDocumentsDirectory – booleanBoy

+0

Oui merci, mais je préférerais avoir des codes d'échantillons afin que je peux me référer. – Lloydworth

+2

google "référence UIImagePickerController". Allez sur le site Web d'Apple. – Akshay

Répondre

32

Cela vous mènera à la galerie d'images et vous pouvez sélectionner l'image.

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

cela vous aidera à sélectionner l'image

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

Et vous pouvez stocker cette image dans le répertoire des documents ...

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

Pour cliquant sur l'image vous utilisez ce

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

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

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

Puis-je savoir comment stocker l'image de l'ImageView dans le répertoire des documents? – Lloydworth

+2

Juste modifié ma réponse ci-dessus ... –

+0

Je viens de penser à une fonction supplémentaire. Dans le premier écran, je vais changer le bouton «Choisir une photo» pour «Parcourir de la galerie de photos» et ajouter un nouveau bouton «Prendre une nouvelle photo». En appuyant sur ce bouton, mon application lancera la caméra et une fois que l'utilisateur aura pris une photo, il retournera au premier écran montrant l'image sur ImageView qui peut aussi être sauvegardée dans mon répertoire de documents. – Lloydworth

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

Et cette méthode déléguée renvoie l'image u sélectionner de l'album

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

où noImage est le UIImageView dans lequel j'affiche l'image sélectionnée dans l'album ... – booleanBoy

+0

Puis-je savoir comment stocker le fichier noImage dans le répertoire des documents? – 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

Ceci est une nouvelle réponse à une vieille question avec une réponse déjà acceptée. Quelle est votre réponse à condition que la réponse acceptée ne le soit pas? De plus, ajouter une explication sur la façon dont ce code fonctionne aiderait les futurs visiteurs. – JAL