UIIMagePickerController dismissViewControllerAnimated:completion:not working

时间:2014-06-04 23:55:28

标签: ios objective-c uiimagepickercontroller

我正在尝试从UIImagePickerController中选择一个图像,然后在UITextView中将其设置为内联。下面的代码非常简单。当我点击“act”按钮时,它会打开UIImagePickerController,一旦完成,委托方法didFinishPickingMediaWithInfo就会被触发。

- (IBAction)act:(id)sender{
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.allowsEditing = YES;
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

    [self presentViewController:picker animated:YES completion:NULL];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
    if(!self.imageView){
        self.imageView = [[UIImageView alloc] initWithImage:chosenImage];
        [self.imageView setContentMode:UIViewContentModeScaleAspectFit];
        [self.imageView setFrame:CGRectMake(10.0, 10.0, 100.0f, 100.0f)];
        [self.content addSubview:self.imageView];
        UIBezierPath *exclusionPath = [UIBezierPath bezierPathWithRect:CGRectMake(0.0,0.0,120.0f,110.0f)];
        self.content.textContainer.exclusionPaths = @[exclusionPath];

    } else {
        [self.imageView setImage:chosenImage];
    }
    [picker dismissViewControllerAnimated:YES completion:nil];
}

问题在于:当我尝试使用dismissViewController解雇UIImagePickerViewController时,它会被忽略。但是,如果没有我设置exclusionPaths的两行,它工作正常。此外,这只发生在UITextView中已有文本时。如果没有文本,我可以根据需要多次执行此操作,并且dismissViewController工作正常。我已经完成了代码,代码实际上已通过[picker dismissViewControllerAnimated: completion:行。此时picker确实是UIImagePickerController的一个实例。任何人都知道发生了什么事?

[更新]我实际上已将代码修改为以下内容:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    UIBezierPath *exclusionPath = [UIBezierPath bezierPathWithRect:CGRectMake(0.0,0.0,120.0f,110.0f)];
    self.content.textContainer.exclusionPaths = @[exclusionPath];
    [picker dismissViewControllerAnimated:YES completion:nil];
}

基本上我甚至不再设置imageView了。只是当我尝试设置UITextView的排除路径 - self.content时 - dismissViewControllerAnimated:completion:不起作用。控制台中没有错误。

1 个答案:

答案 0 :(得分:1)

您必须在dismissViewConroller:而不是self致电picker

更好的是,请在picker.presentingViewController上调用它:

[picker.presentingViewController dismissViewControllerAnimated:YES completion:nil];