user2003416
user2003416

Reputation: 159

Taking photo through default camera

I am using UIImagePickerView controller to take photo from iPhone default camera in my App. It is taking long time to complete the process.

-(IBAction)takePhoto:(id)sender
{
    if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera])
    {
        imgPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
        [self presentModalViewController:imgPicker animated:YES];
    }
}

-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *pickedImage = [info objectForKey:UIImagePickerControllerOriginalImage];    
    [self dismissModalViewControllerAnimated:YES];

    NSData *imageData = UIImagePNGRepresentation(pickedImage);
    path = [SAVEDIMAGE_DIR stringByAppendingPathComponent:@"image.png"];
    [imageData writeToFile:path atomically:YES];
    [tableview reloadData];
}

Upvotes: 2

Views: 115

Answers (2)

Nitin Gohel
Nitin Gohel

Reputation: 49730

you can capture image from default camera like bellow method:-

-(IBAction)cameraLibraryButtonClick:(id)sender{


    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){

                imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
                imagePicker.mediaTypes = [NSArray arrayWithObjects:(NSString *)kUTTypeImage , nil];
                imagePicker.allowsEditing = NO;

                [self presentModalViewController:imagePicker animated:YES];

                newMedia = YES;
            }
            else
            {
                [self displaysorceError];
            }


}

-(void)displaysorceError{
    UIAlertView *alt = [[UIAlertView alloc] 
                        initWithTitle:@"Error" 
                        message:@"Camera Image Sorce Not Available" 
                        delegate:nil cancelButtonTitle:@"OK" 
                        otherButtonTitles:nil];
    [alt show];
    [alt release];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    NSLog(@"Media Info: %@", info);
    NSString *mediaType = [info valueForKey:UIImagePickerControllerMediaType];

    if([mediaType isEqualToString:(NSString*)kUTTypeImage]) {
        UIImage *photoTaken = [info objectForKey:@"UIImagePickerControllerOriginalImage"];

        //Save Photo to library only if it wasnt already saved i.e. its just been taken
        if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
            UIImageWriteToSavedPhotosAlbum(photoTaken, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
        }


    }

    [picker dismissModalViewControllerAnimated:YES];
    [picker release];
}

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
    UIAlertView *alert;
    //NSLog(@"Image:%@", image);
    if (error) {
        alert = [[UIAlertView alloc] initWithTitle:@"Error!"
                                           message:[error localizedDescription]
                                          delegate:nil
                                 cancelButtonTitle:@"OK"
                                 otherButtonTitles:nil];
        [alert show];
        [alert release];
    }

}

Upvotes: 1

Vishal
Vishal

Reputation: 8256

Try these lines:

UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];

imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;

imagePicker.delegate = self;

[self presentViewController:imagePicker animated:YES completion:nil];

Upvotes: 2

Related Questions