3

我有一个问题:

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

只被相机调用,而不是当我试图通过浏览用户的照片来选择图像时。

这是我的 UIImagePicker 委托的初始化代码:

-(void)viewDidLoad{

    [self initImagePicker];

}


-(void)initImagePicker{
    DLog(@"");

    self.imagePickerController.delegate = self;

}



- (void)setupImagePicker:(UIImagePickerControllerSourceType)sourceType
{
    self.imagePickerController.sourceType = sourceType;

    if (sourceType == UIImagePickerControllerSourceTypeCamera)
    {
        // user wants to use the camera interface
        //
        self.imagePickerController.showsCameraControls = NO;

        if ([[self.imagePickerController.cameraOverlayView subviews] count] == 0)
        {
            // setup our custom overlay view for the camera
            //
            // ensure that our custom view's frame fits within the parent frame
            CGRect overlayViewFrame = self.imagePickerController.cameraOverlayView.frame;

            CGRect newFrame = CGRectMake(0.0,CGRectGetHeight(overlayViewFrame)-15.0,CGRectGetWidth(overlayViewFrame),15.0);

            self.view.frame = newFrame;
            [self.imagePickerController.cameraOverlayView addSubview:self.view];
            [self.view bringSubviewToFront:self.view];
        }
    }
}

然后在我的根类中,它是 UIImagePicker 类的委托,它是 UIImagePicker 的委托:

#pragma mark -
#pragma mark Toolbar Actions

- (void)showImagePicker:(UIImagePickerControllerSourceType)sourceType
{




    if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
    {
        // camera is not on this device, don't show the camera button
        NSMutableArray *toolbarItems = [NSMutableArray arrayWithCapacity:self.myToolbar.items.count];
        [toolbarItems addObjectsFromArray:self.myToolbar.items];
        [toolbarItems removeObjectAtIndex:2];
        [self.myToolbar setItems:toolbarItems animated:NO];
    }

    if ([UIImagePickerController isSourceTypeAvailable:sourceType])
    {


        self.overlayViewController = [[UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil] instantiateViewControllerWithIdentifier:@"OverlayViewController"];
        self.overlayViewController.delegate = self;

        self.overlayViewController.imagePickerController = [[UIImagePickerController alloc] init];
        [self.overlayViewController setupImagePicker:sourceType];
        [self presentModalViewController:self.overlayViewController.imagePickerController animated:YES];
    }
}

我知道这是一个 SO 问题的很多代码,但是对于有实现这个类的经验的人来说,我做错了什么?

对我来说,这条线很奇怪:

 self.overlayViewController.imagePickerController = [[UIImagePickerController alloc] init];

我以前从未在所有者之外分配和初始化属性。是这个问题吗?

4

3 回答 3

6

我认为您设置代表太早了(initImagePickerviewDidLoad)。self.imagePickerController那里可能为零。您应该在创建UIImagePickerController.

于 2013-02-17T12:04:29.603 回答
1

找到了修复:

需要补充:

self.overlayViewController.imagePickerController.delegate = self.overlayViewController;

后:

self.overlayViewController.imagePickerController = [[UIImagePickerController alloc] init];
于 2013-02-17T12:04:36.033 回答
1

将 .h 文件中的 UIImagePickerDelegate 和 UINavigationControllerDelegate 设置为 @interface。

就像

@interface class : UIView <UIImagePickerDelegate , UINavigationControllerDelegate>

使用 UIImagePickerController 对象设置自我委托,

imgPickerController.delegate = self;

希望对你也有帮助。

于 2013-02-17T17:44:06.747 回答