9

In iPhone Apps, I want to take pictures from Camera & save into Photo Gallery by programmatically. Is it possible in iPhone Simulator? so please tell me any link or materials you have.

Thanks in advance

Man of One Way
  • 3,904
  • 1
  • 26
  • 41
Nikunj Jadav
  • 105
  • 1
  • 1
  • 7

4 Answers4

18

You would use uiimagepickercontroller for this purpose. First of all, import the MobileCoreServices.framework. Then pull up the camera:

if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        NSArray *media = [UIImagePickerController
                          availableMediaTypesForSourceType: UIImagePickerControllerSourceTypeCamera];

        if ([media containsObject:(NSString*)kUTTypeImage] == YES) {
            UIImagePickerController *picker = [[UIImagePickerController alloc] init];
            picker.sourceType = UIImagePickerControllerSourceTypeCamera;
            //picker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
            [picker setMediaTypes:[NSArray arrayWithObject:(NSString *)kUTTypeImage]];

            picker.delegate = self;
            [self presentModalViewController:picker animated:YES];
            //[picker release];

        }
        else {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Unsupported!"
                                                            message:@"Camera does not support photo capturing."
                                                           delegate:nil
                                                  cancelButtonTitle:@"OK"
                                                  otherButtonTitles:nil];
            [alert show];
            [alert release];
        }

    }
    else {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Unavailable!"
                                                        message:@"This device does not have a camera."
                                                       delegate:nil
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
        [alert show];
        [alert release];
    }

Then, save the photo by implementing the uiimagepickercontrollerdelegate and a save function:

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

}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [picker dismissModalViewControllerAnimated:YES];
}

More info on image:didFinishSaving.... can be found here

http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIKitFunctionReference/Reference/reference.html

Also have a look at this post

https://stackoverflow.com/questions/1282830/uiimagepickercontroller-uiimage-memory-and-more

Community
  • 1
  • 1
Bushra Shahid
  • 3,579
  • 1
  • 27
  • 37
  • this is working for me correctly..my captured images are saving successfully to the device gallery but i want to know that Is it possible to save images in a particular folder(like a new album) of a gallery? thanks – Anand Gautam Mar 27 '14 at 05:48
5

it is not possible in simulator.You have to use the device.

Praveen S
  • 10,355
  • 2
  • 43
  • 69
Heena
  • 754
  • 5
  • 18
  • 30
2

I do hope you realize that the simulator does not have a camera..... So I guess thats not possible. You can definitely use a device.... You can make use of UIImagePickerController for capturing images.

visakh7
  • 26,380
  • 8
  • 55
  • 69
0

There are 3 prerequesites needed to take a picture programmatically:

  1. The device needs to have a camera check by

     if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
    
  2. Camera controls need to be hidden (picker is a UIImagePickerController)

    picker.showsCameraControls = NO;
    
  3. use the takePicture from UIImagePickerController

    [picker takePicture];

Side note because it did bug me for a few minutes, the takePicture method also dismisses the view.

Also posted a more complete answer here Other Stack answer

Community
  • 1
  • 1
GrandSteph
  • 2,053
  • 1
  • 16
  • 23