Narender Tak
Narender Tak

Reputation: 241

Camera not presenting in ipad running iOS 8.2

I am working on universal app and want to open camera. Now I am able to open camera with UIimagePickerController on iphone but on iPad its not working. I have searched for solution and found this code

self.cameraController = [[UIImagePickerController alloc] init];
self.cameraController.sourceType = UIImagePickerControllerSourceTypeCamera;


    self.cameraController.modalPresentationStyle = UIModalPresentationCurrentContext;

self.cameraController.showsCameraControls = NO;
self.cameraController.navigationBarHidden = YES;
self.cameraController.wantsFullScreenLayout = NO;

[self.cameraController setCameraOverlayView:ar_overlayView];
[ar_overlayView setFrame:self.cameraController.view.bounds];

UIViewController *vc = [[[[UIApplication sharedApplication] delegate] window] rootViewController];
if([[[UIDevice currentDevice] systemVersion] floatValue]>=8.0 && UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
    [[NSOperationQueue mainQueue] addOperationWithBlock:^{

        [self presentViewController:self.cameraController animated:YES completion:nil];
    }];

}
else{
    [vc presentViewController:self.cameraController animated:YES completion:nil];
}

Also followed some tutorial like

TechnoTopia Post but didn't find any luck.I have tested it in iphone 5s and is working fine in it but on iPad mini the camera is not presenting. Any help would be appreciated!

Upvotes: 1

Views: 299

Answers (4)

Hitesh Surani
Hitesh Surani

Reputation: 13537

- (IBAction)onClickSelectPicture:(id)sender 
{
    if( [UIImagePickerController isCameraDeviceAvailable: UIImagePickerControllerCameraDeviceFront ])
    {
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.delegate = self;
        picker.allowsEditing = YES;
        picker.sourceType=UIImagePickerControllerSourceTypeCamera;
        picker.cameraDevice=UIImagePickerControllerCameraDeviceFront;
        [self presentViewController:picker animated:YES completion:NULL];
    }
    else
    {
        UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Error"
                                                              message:@"Device has no camera"
                                                             delegate:nil
                                                    cancelButtonTitle:@"OK"
                                                    otherButtonTitles: nil];

        [myAlertView show];
    }
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{

    imgProf = info[UIImagePickerControllerEditedImage];
        [viewNewPhoto setImage:imgProf];

    imgProf = [self imageWithImage:imgProf scaledToSize:CGSizeMake(imgProf.size.width/4, imgProf.size.height/4)];

    if (imgProf == nil)
    {
        imgPhotoString = @"NoPhoto";
    }else
    {
        imgPhotoString = [self getStringFromImage:imgProf];
    }

    [picker dismissViewControllerAnimated:YES completion:NULL];
}

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

Upvotes: 0

Try This For iPad

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex==0)
{
     [[NSOperationQueue mainQueue] addOperationWithBlock:^{
 UIImagePickerController* picker=[[UIImagePickerController alloc]init];
    picker.delegate=self;
    picker.allowsEditing=YES;
    picker.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
    [self presentViewController:picker animated:YES completion:nil];
     }];
}
else if (buttonIndex==1)
{
     [[NSOperationQueue mainQueue] addOperationWithBlock:^{
 UIImagePickerController* picker=[[UIImagePickerController alloc]init];
    picker.delegate=self;
    picker.allowsEditing=YES;
    picker.sourceType=UIImagePickerControllerSourceTypeCamera;
         [self presentViewController:picker animated:YES completion:nil];}];
}
}

Upvotes: 1

Shivani Gor
Shivani Gor

Reputation: 329

try this code :

UIImagePickerController *aImgPickerController = [[UIImagePickerController alloc] init];
aImgPickerController.delegate = self;
aImgPickerController.allowsEditing = YES;

aImgPickerController.sourceType = UIImagePickerControllerSourceTypeCamera;

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    [self presentViewController:aImgPickerController animated:NO completion:^{
        NSLog(@"success present picker");
    }];
});

Upvotes: 0

maddy
maddy

Reputation: 4111

For iPad you should do:

if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:picker];
[popover presentPopoverFromRect:self.selectedImageView.bounds inView:self.selectedImageView permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
self.popOver = popover;
}else {
[self presentModalViewController:picker animated:YES];
}

where in .h file declare:

@property (nonatomic, strong) UIPopoverController *popOver;

You should handle popover delegate:

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 

-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker 

Apple docs: apple link

it states that:

"Present the user interface by calling the presentViewController:animated:completion: method of the currently active view controller, passing your configured image picker controller as the new view controller. On iPad, present the user interface using a popover. Doing so is valid only if the sourceType property of the image picker controller is set to UIImagePickerControllerSourceTypeCamera."

Upvotes: 0

Related Questions