Use the UIImagePickerController on a iphone simula

2019-04-08 04:29发布

I have the method, that take photos from gallery or from the camera

-(IBAction) getPhoto:(id) sender {
  UIImagePickerController * picker = [[UIImagePickerController alloc] init];
  picker.delegate = self;

  if((UIButton *) sender == choosePhotoBtn) {
        picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
  } else {
        picker.sourceType = UIImagePickerControllerSourceTypeCamera;
  }

  [self presentModalViewController:picker animated:YES];
}

But when i run it on the simulator, code doesnt work. And it doesnt work in picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum and picker.sourceType = UIImagePickerControllerSourceTypeCamera

Is the problem in the simulator or in the code?

4条回答
再贱就再见
2楼-- · 2019-04-08 05:02

Similarly to the above answers, but I found this easier. Show a pop up alert if the device doesn't have a camera (like the simulator). Sam code, different usage:

//button if to take a photo
- (IBAction)takePhoto:(id)sender {

//checks if device has a camera
    if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        UIAlertView *noCameraAlert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"You don't have a camera for this device" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];

        //shows above alert if there's no camera
        [noCameraAlert show];
    }

    //otherwise, show a modal for taking a photo
    else {
        UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
        imagePicker.delegate = self;
        imagePicker.allowsEditing = YES;
        imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;

        [self presentViewController:imagePicker animated:YES completion:NULL];
    }
}
查看更多
狗以群分
3楼-- · 2019-04-08 05:10

In simulator your picker.sourceType = UIImagePickerControllerSourceTypeCamera wont be called as there is no camera available in simulator. Also its a good practice to check whether the source type is available to avoid crashes.

#import <MobileCoreServices/UTCoreTypes.h>
….
 if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
        {
            UIImagePickerController *imagePickerCamera =[[UIImagePickerController alloc] init];
            imagePickerCamera.delegate = self;
            imagePickerCamera.mediaTypes = [NSArray arrayWithObjects:(NSString *) kUTTypeImage,nil];
            imagePickerCamera.allowsEditing = YES;
            imagePickerCamera.sourceType = UIImagePickerControllerSourceTypeCamera;

            [self presentViewController:imagePickerCamera  animated:YES completion:nil];
        }

    else if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum])
        {
            UIImagePickerController *imagePickerAlbum =[[UIImagePickerController alloc] init];
            imagePickerAlbum.delegate = self;
            imagePickerAlbum.mediaTypes = [NSArray arrayWithObjects:(NSString *) kUTTypeImage,nil];
            imagePickerAlbum.allowsEditing = YES;
            imagePickerAlbum.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

            [self presentViewController:imagePickerAlbum animated:YES completion:nil];
        }
查看更多
放荡不羁爱自由
4楼-- · 2019-04-08 05:13

Try this,

 if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
        {
            picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        }
        else
        {
            picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
        }
        [self.navigationController presentModalViewController:picker animated:NO];

If you are creating the app for iPad. You will have to present the gallery in a popOver control.

查看更多
贼婆χ
5楼-- · 2019-04-08 05:19

Swift 2.2 version:

if UIImagePickerController.isSourceTypeAvailable(.Camera) {
    picker!.sourceType = .Camera
}
else {
    picker!.sourceType = .SavedPhotosAlbum // or .PhotoLibrary
}

Swift 3.0 verison:

if UIImagePickerController.isSourceTypeAvailable(.camera) {
    picker.sourceType = .camera
}
else {
    picker.sourceType = .savedPhotosAlbum // or .photoLibrary
}

In simulator, you should also not set cameraCaptureMode nor showsCameraControls.

查看更多
登录 后发表回答