UIImagePickerController相机拍摄的照片是空白的怎么办?

3
我正在尝试使用UIImagePickerControllerSourceTypeCamera拍照。在拍摄快照后,我得到的是一张空白照片,但如果我选择使用照片,我可以在我的UIImage中看到它:enter image description here同时,控制台中出现了以下错误信息:

: CGAffineTransformInvert: singular matrix.

Snapshotting a view that has not been rendered results in an empty snapshot. Ensure your view has been rendered at least once before snapshotting or snapshot after screen updates.

这是我的代码:

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
 }

- (void)viewDidAppear:(BOOL)animated {

  [super viewDidAppear:animated];

}

- (void)didReceiveMemoryWarning
{
  [super didReceiveMemoryWarning];
  // Dispose of any resources that can be recreated.
}

- (IBAction)takePhoto:(id)sender
{

if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
    UIImagePickerController *imagePicker =[[UIImagePickerController alloc] init];
    imagePicker.delegate = self;
    imagePicker.sourceType =UIImagePickerControllerSourceTypeCamera;
    imagePicker.mediaTypes = @[(NSString *) kUTTypeImage];
    imagePicker.allowsEditing = NO;
    [self presentViewController:imagePicker
                       animated:YES completion:nil];
    _newMedia = YES;
}
}


 - (IBAction)selectPhoto:(id)sender
 {

if ([UIImagePickerController isSourceTypeAvailable:
     UIImagePickerControllerSourceTypeSavedPhotosAlbum])
{
    UIImagePickerController *imagePicker =
    [[UIImagePickerController alloc] init];
    imagePicker.delegate = self;
    imagePicker.sourceType =
    UIImagePickerControllerSourceTypePhotoLibrary;
    imagePicker.mediaTypes = @[(NSString *) kUTTypeImage];
    imagePicker.allowsEditing = NO;
    [self presentViewController:imagePicker
                       animated:YES completion:nil];
    _newMedia = NO;
}

}

#pragma mark UIImagePickerControllerDelegate

-(void)imagePickerController:(UIImagePickerController *)picker
 didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSString *mediaType = info[UIImagePickerControllerMediaType];

[self dismissViewControllerAnimated:YES completion:nil];

if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
    UIImage *image = info[UIImagePickerControllerOriginalImage];

    _imageView.image = image;
    if (_newMedia)
        UIImageWriteToSavedPhotosAlbum(image,
                                       self,


  @selector(image:finishedSavingWithError:contextInfo:),
                                       nil);
}
else if ([mediaType isEqualToString:(NSString *)kUTTypeMovie])
{
    // Code here to support video if enabled
}
}

-(void)image:(UIImage *)image finishedSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
if (error) {
    UIAlertView *alert = [[UIAlertView alloc]
                          initWithTitle: @"Save failed"
                          message: @"Failed to save image"
                          delegate: nil
                          cancelButtonTitle:@"OK"
                          otherButtonTitles:nil];
    [alert show];
}
}

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

有趣,看起来像是iOS的问题。我以前从未遇到过这种情况,你尝试在其他设备上了吗? - Jack
你的应用程序是否具有相机权限? - Adil Soomro
4个回答

0

我遇到了同样的问题。如果我将相机代码复制到一个新项目中,它可以正常工作。所以我认为可能不是代码的问题。 最后,在我的项目中,我发现这是由于一个关于状态栏的第三方库引起的。

    self.MTStateBar = [[[MTStatusBarOverlay alloc] initWithFrame:CGRectMake(0, 0, 320, 20)] autorelease];

我通过替换解决了这个问题。希望这个答案能对你有所帮助。


0
我通过 DevC 的评论找到了解决问题的方法,指明了正确的方向。我使用了一个 ActionSheet 来呈现 UIImagePickerController,因此在单击其上的按钮时,动画仍在运行,同时选择器也被打开。
我通过在运行触发选择器的代码之前关闭选择器来解决了这个问题:
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    [actionSheet dismissWithClickedButtonIndex:999 animated:NO]; //non-existing button index so it doesn't trigger anything again

    // Rest of code here (UIImagePickerController trigger)
}

0

我已经测试了你的代码,它运行良好。我认为你面临的问题与这个问题有关...

对未呈现的视图进行快照会导致空快照。在快照或屏幕更新后进行快照之前,请确保您的视图已经呈现至少一次。

请查看这篇文章;这可能会帮助你... UIImagePickerController error: Snapshotting a view that has not been rendered results in an empty snapshot in iOS 7

这是完整的代码,以防你漏掉了什么...
ViewController.h

#import <UIKit/UIKit.h>
@interface ViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate>
@property (nonatomic, weak) IBOutlet UIImageView *imageView;
- (IBAction)takePhoto:(id)sender;
- (IBAction)selectPhoto:(id)sender;
@end


ViewController.m

#import "ViewController.h"
@import MobileCoreServices;

@interface ViewController ()
@property (nonatomic, readwrite) BOOL newMedia;
@end

@implementation ViewController
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)viewDidAppear:(BOOL)animated {

    [super viewDidAppear:animated];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)takePhoto:(id)sender
{

    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
    {
        UIImagePickerController *imagePicker =[[UIImagePickerController alloc] init];
        imagePicker.delegate = self;
        imagePicker.sourceType =UIImagePickerControllerSourceTypeCamera;
        imagePicker.mediaTypes = @[(NSString *) kUTTypeImage];
        imagePicker.allowsEditing = NO;
        [self presentViewController:imagePicker
                           animated:YES completion:nil];
        _newMedia = YES;
    }
}


- (IBAction)selectPhoto:(id)sender
{

    if ([UIImagePickerController isSourceTypeAvailable:
         UIImagePickerControllerSourceTypeSavedPhotosAlbum])
    {
        UIImagePickerController *imagePicker =
        [[UIImagePickerController alloc] init];
        imagePicker.delegate = self;
        imagePicker.sourceType =
        UIImagePickerControllerSourceTypePhotoLibrary;
        imagePicker.mediaTypes = @[(NSString *) kUTTypeImage];
        imagePicker.allowsEditing = NO;
        [self presentViewController:imagePicker
                           animated:YES completion:nil];
        _newMedia = NO;
    }

}

//- (IBAction)takePhoto_

#pragma mark UIImagePickerControllerDelegate

-(void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    NSString *mediaType = info[UIImagePickerControllerMediaType];

    [self dismissViewControllerAnimated:YES completion:nil];

    if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
        UIImage *image = info[UIImagePickerControllerOriginalImage];

        _imageView.image = image;
        if (_newMedia)
            UIImageWriteToSavedPhotosAlbum(image,
                                           self,


                                           @selector(image:finishedSavingWithError:contextInfo:),
                                           nil);
    }
    else if ([mediaType isEqualToString:(NSString *)kUTTypeMovie])
    {
        // Code here to support video if enabled
    }
}

-(void)image:(UIImage *)image finishedSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    if (error) {
        UIAlertView *alert = [[UIAlertView alloc]
                              initWithTitle: @"Save failed"
                              message: @"Failed to save image"
                              delegate: nil
                              cancelButtonTitle:@"OK"
                              otherButtonTitles:nil];
        [alert show];
    }
}

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

@end

1
我的代码和你的完全一样。我已经看过你提到的帖子,但是仍然没有一个答案适用于我。 - man_or_astroman
1
@lucky13 不确定你是否解决了这个错误,如果你解决了,恭喜你,因为它仍然出现在iOS8中。有时甚至会出现在苹果的示例代码中。您可以尝试延迟启动相机。我发现如果其他转换仍在后台完成,这个问题经常会发生。 - DevC

0
更新 3/15: 你是否使用 UIActionSheet 来展示 UIImagePickerController?我通过将 UIActionSheet 替换为 UIAlertController 来解决了这个问题。因为 UIAlertView 可能会导致意想不到的 bug。不要再使用 UIAlertView 了 ;)
我认为这是 iOS 的一个 bug,你可以延迟 0.5 秒来呈现 UIImagePickerController,这对我很有效。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接