未调用UIImagePickerController委托方法(委托设置为self)

时间:2013-02-17 11:47:45

标签: iphone ios objective-c uiimagepickercontroller

我遇到问题:

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

我以前从未在业主之外分配和初始化房产。这是问题吗?

3 个答案:

答案 0 :(得分:6)

我认为您过早地设置了委托(initImagePicker中的viewDidLoad)。 self.imagePickerController可能在那里没有。 您应该在创建UIImagePickerController

后设置委托

答案 1 :(得分:1)

找到了修复:

需要添加:

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

后:

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

答案 2 :(得分:1)

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

一样
@interface class : UIView <UIImagePickerDelegate , UINavigationControllerDelegate>

使用UIImagePickerController对象设置自委托,

imgPickerController.delegate = self;

希望它也能帮到你。