2008-09-23 16 views

Respuesta

4

La clase UIImagePickerController le permite tomar fotografías o elegirlos de la biblioteca de fotos. Especifique el tipo de fuente como UIImagePickerControllerSourceTypeCamera.

Ver también esta pregunta hecha anteriormente: Access the camera with iPhone SDK

11

Uso UIImagePickerController. Hay un buen tutorial sobre esto aquí.

http://www.zimbio.com/iPhone/articles/1109/Picking+Images+iPhone+SDK+UIImagePickerController

debe establecer el tipo de fuente de UIImagePickerControllerSourceTypeCamera o UIImagePickerControllerSourceTypePhotoLibrary. Tenga en cuenta que estos dos tipos resultan en pantallas muy diferentes en la pantalla. Debe probar ambos cuidadosamente. En particular, si anida el UIImagePickerController dentro de un UINavigationController, puede terminar con barras de navegación múltiples y otros efectos extraños si no tiene cuidado.

Ver también this thread

24

Simplemente copia y pega el código siguiente en su proyecto para obtener la funcionalidad totalmente implementado.

donde takePhoto y chooseFromLibrary son mis propios nombres de los métodos que serán tratados en el botón táctil.

Asegúrese de hacer referencia a las salidas de los botones correspondientes a estos métodos.

-(IBAction)takePhoto :(id)sender 

{ 
     UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init]; 

     if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) 
     { 
      [imagePickerController setSourceType:UIImagePickerControllerSourceTypeCamera]; 
     } 

     // image picker needs a delegate, 
     [imagePickerController setDelegate:self]; 

    // Place image picker on the screen 
    [self presentModalViewController:imagePickerController animated:YES]; 
} 



-(IBAction)chooseFromLibrary:(id)sender 
{ 

    UIImagePickerController *imagePickerController= [[UIImagePickerController alloc] init]; 
    [imagePickerController setSourceType:UIImagePickerControllerSourceTypePhotoLibrary]; 

    // image picker needs a delegate so we can respond to its messages 
    [imagePickerController setDelegate:self]; 

    // Place image picker on the screen 
    [self presentModalViewController:imagePickerController animated:YES]; 

} 

//delegate methode will be called after picking photo either from camera or library 
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{ 
    [self dismissModalViewControllerAnimated:YES]; 
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage]; 

    [myImageView setImage:image]; // "myImageView" name of any UIImageView. 
} 
1

respuesta Publicado por @WQS funciona bien, pero contiene algunos métodos que están en desuso desde iOS 6. Aquí está la respuesta actualizada para iOS 6 & arriba:

-(void)takePhoto 
{ 
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init]; 

    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) 
    { 
     [imagePickerController setSourceType:UIImagePickerControllerSourceTypeCamera]; 
    } 

    // image picker needs a delegate, 
    [imagePickerController setDelegate:self]; 

    // Place image picker on the screen 
    [self presentViewController:imagePickerController animated:YES completion:nil]; 
} 



-(void)chooseFromLibrary 
{ 

    UIImagePickerController *imagePickerController= [[UIImagePickerController alloc]init]; 
    [imagePickerController setSourceType:UIImagePickerControllerSourceTypePhotoLibrary]; 

    // image picker needs a delegate so we can respond to its messages 
    [imagePickerController setDelegate:self]; 

    // Place image picker on the screen 
    [self presentViewController:imagePickerController animated:YES completion:nil]; 

} 

//delegate methode will be called after picking photo either from camera or library 
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{ 
    [self dismissViewControllerAnimated:YES completion:nil]; 
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage]; 

[myImageView setImage:image]; // "myImageView" name of any UImageView. 
} 

No se olvide de añadir esto en su view controller.h:

@interface myVC<UINavigationControllerDelegate, UIImagePickerControllerDelegate> 
0

Aquí está mi código que he utilizado para tomar la foto de mi aplicación

- (IBAction)takephoto:(id)sender { 

    picker = [[UIImagePickerController alloc] init]; 
    picker.delegate = self; 
    [picker setSourceType:UIImagePickerControllerSourceTypeCamera]; 
    [self presentViewController:picker animated:YES completion:NULL]; 


} 
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{ 
    img = [info objectForKey:@"UIImagePickerControllerOriginalImage"]; 
    [imageview setImage:img]; 
    [self dismissViewControllerAnimated:YES completion:NULL]; 
} 

si desea volver a tomar la imagen sólo simple agregar esta función

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