当图片被点击时,图像选择器不会消失

时间:2012-11-30 03:43:49

标签: iphone objective-c ipad

当点击了条形按钮项时,弹出视图显示正常,但在选择照片时不会忽略。我错过了什么吗?我该怎么办?

-(IBAction)addPhoto:(UIBarButtonItem *)sender
{
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    popover = [[UIPopoverController alloc] initWithContentViewController:imagePicker];
    [popover presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)selectedImage editingInfo:(NSDictionary *)editingInfo
{   
    // Delete any existing image.
    NSManagedObject *oldImage = imageClass.image;
    if (oldImage != nil)
    {
        [imageClass.managedObjectContext deleteObject:oldImage];
    }

    // Create an image object for the new image.
    NSManagedObject *myImage = [NSEntityDescription insertNewObjectForEntityForName:@"Image" inManagedObjectContext:imageClass.managedObjectContext];
    imageClass.image = myImage;

    // Set the image for the image managed object.
    [image setValue:selectedImage forKey:@"image"];

    [popover dismissPopoverAnimated:YES];
}


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

7 个答案:

答案 0 :(得分:2)

问题是你试图解雇图像选择器控制器,但是你需要关掉它所在的弹出窗口。事情需要根据它们的呈现方式被解雇。

变化:

[self dismissViewControllerAnimated:YES completion:nil];

为:

[popover dismissPopoverAnimated:YES];

如果您拨打了dismissViewControllerAnimated:completion:,则会使用presentViewController:animated:completion:

答案 1 :(得分:1)

要从库中选择照片时关闭UIPopoverViewController,您需要在选择图像后在didFinishPickingMediaWithInfo方法中添加以下行。

- (void)imagePickerController:(UIImagePickerController *)imagePicker didFinishPickingMediaWithInfo:(NSDictionary *)info {
     [popover dismissPopoverAnimated:YES];
     [imagePicker dismissModalViewControllerAnimated:YES];
}

答案 2 :(得分:0)

您尚未将delegate的{​​{1}}设置为自我。另外,您需要将imagePicker作为实例变量以在委托方法中解除。

答案 3 :(得分:0)

1)在你.h你必须添加<UIImagePickerControllerDelegate> 2)添加imagePicker.delegate=self;,因此您的addPhoto方法应如下所示:

-(IBAction)addPhoto:(UIBarButtonItem *)sender {
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate=self;  //add this line to your code
popover = [[UIPopoverController alloc]
           initWithContentViewController:imagePicker];

[popover presentPopoverFromBarButtonItem:sender
                permittedArrowDirections:UIPopoverArrowDirectionAny
                                animated:YES];
}

答案 4 :(得分:0)

只需在UIPopoverControllerDelegate文件

中提供代理UIImagePickerControllerDelegate.h

答案 5 :(得分:0)

您必须将UIImagePickerControllerDelegate添加到相应的.h文件中并检查此代码

 -(IBAction)addPhoto:(UIBarButtonItem *)sender 
{
  UIImagePickerController *imagePicker3 = [[UIImagePickerController alloc] init];
  imagePicker3.delegate = self;
  imagePicker3.sourceType = UIImagePickerControllerSourceTypeCamera;
  imagePicker3.allowsEditing = YES;
  imagePicker3.mediaTypes = [NSArray arrayWithObject:@"public.image"]
  [self presentModalViewController:imagePicker3 animated:YES];

}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { 
[self dismissModalViewControllerAnimated:YES];
_imageselected.image=[info valueForKey:UIImagePickerControllerEditedImage];

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

 }

答案 6 :(得分:0)

如果在使用UIImagePickerController时有人仍然遇到这个臭名昭着的错误:

Assigning to 'id <UINavigationControllerDelegate,UIImagePickerControllerDelegate>' from   incompatible type 'DetailViewController *const __strong'

尝试移动

UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;

- (void)viewDidLoad{
  //do your stuff
}

并且不要忘记在相应的.h文件中包含委托协议定义(&lt; UINavigationControllerDelegate,UIImagePickerControllerDelegate&gt;)。

这解决了我的问题,希望它会帮助别人。不知道它是否是最好的解决方案,但在类似情况下为我工作。