在 iPhone 模拟器上使用 UIImagePickerController

发布于 2025-01-03 12:51:18 字数 681 浏览 2 评论 0原文

我有一种方法,可以从图库或相机中拍摄照片,

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

但是当我在模拟器上运行它时,代码不起作用。它在 picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbumpicker.sourceType = UIImagePickerControllerSourceTypeCamera 中不起作用

问题出在模拟器中还是代码中?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

左秋 2025-01-10 12:51:18

,请尝试此操作

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

如果您正在为 iPad 创建应用程序 。您必须在 popOver 控件中显示图库。

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.

压抑⊿情绪 2025-01-10 12:51:18

Swift 3/4/5 版本:

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

Swift 2 版本:

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

模拟器中,您不能使用cameraCaptureModeshowsCameraControls

Swift 3/4/5 verison:

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

Swift 2 version:

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

In simulator, you can't use cameraCaptureMode and showsCameraControls.

孤凫 2025-01-10 12:51:18

在模拟器中,您的 picker.sourceType = UIImagePickerControllerSourceTypeCamera 将不会被调用,因为模拟器中没有可用的相机。检查源类型是否可用以避免崩溃也是一个好习惯。

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

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];
        }
与他有关 2025-01-10 12:51:18

与上面的答案类似,但我发现这更容易。如果设备没有摄像头(如模拟器),则显示弹出警报。 Sam代码,不同用法:

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

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];
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文