Iphone 点击图片时图像选择器未关闭

Iphone 点击图片时图像选择器未关闭,iphone,objective-c,ipad,Iphone,Objective C,Ipad,点击条形按钮项目时,弹出视图显示良好,但在选择照片时不会关闭。我错过什么了吗?我该怎么办 -(IBAction)addPhoto:(UIBarButtonItem *)sender { UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init]; popover = [[UIPopoverController alloc] initWithContentViewController:i

点击条形按钮项目时,弹出视图显示良好,但在选择照片时不会关闭。我错过什么了吗?我该怎么办

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

问题是,您试图关闭图像选择器控制器,但需要关闭它所在的popover。事情需要根据其呈现方式来处理

更改:

[self dismissViewControllerAnimated:YES completion:nil];
致:


如果调用了
presentViewController:animated:completion:
,则将使用调用
dismissviewcontroller:animated:completion:
问题是您试图解除图像选择器控制器,但需要解除其所在的popover。事情需要根据其呈现方式来处理

更改:

[self dismissViewControllerAnimated:YES completion:nil];
致:


调用
dismissViewControllerAnimated:completion:
将用于调用
presentViewController:animated:completion:
您尚未将
图像采集器的
委托
设置为self。另外,您需要将
popover
作为实例变量,以便在委派方法中排除。

您尚未将
imagePicker
delegate
设置为self。另外,您需要将
popover
作为实例变量,以便在委托方法中排除。

1)。h您必须添加
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];
}
1) 在you.h中,必须添加
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];
}

只需在
.h
文件中给代理
UIPopoverControllerDelegate
UIImagePickerControllerDelegate
.h
文件中给代理
UIPopoverControllerDelegate
UIImagePickerControllerDelegate
,即可在选择一个从“多媒体资料”中选择照片后,您需要在
didFinishPickingMediaWithInfo
方法中添加以下行

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

要在从“多媒体资料”中选择照片时关闭UIPopoverViewController,您需要在拾取图像后在
didFinishPickingMediaWithInfo
方法中添加以下行

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

您必须将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];

 }

您必须将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];

 }

如果有人在使用UIImagePickerController时仍遇到此臭名昭著的错误:

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

- (void)viewDidLoad{
  //do your stuff
}
不要忘记在相应的.h文件中包含委托协议定义()


这解决了我的问题,希望能帮助别人。不知道这是否是最好的解决方案,但在类似的情况下对我有效。

如果有人在使用UIImagePickerController时仍遇到此臭名昭著的错误:

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

- (void)viewDidLoad{
  //do your stuff
}
不要忘记在相应的.h文件中包含委托协议定义()



这解决了我的问题,希望能帮助别人。不知道这是否是最好的解决方案,但在类似的情况下对我有效。

没有效果如何?那件府绸还在视线中还是别的什么?验证当到达图像选取器委托方法时,
popover
是否为
nil
或不在该方法内。该方法保持在视图中。什么都没变我剩下的问题呢?在您尝试调用
dismissPopoverAnimated:
时,您是否验证了
popover
不是
nil
?请使用调试器。在该行设置断点。当到达断点时,查看
popover
的值。如果看不到最新的代码,也不知道您实际在做什么,就很难进一步提供帮助。您是否更改了这两种图像选取器委托方法?不起作用如何?那件府绸还在视线中还是别的什么?验证当到达图像选取器委托方法时,
popover
是否为
nil
或不在该方法内。该方法保持在视图中。什么都没变我剩下的问题呢?在您尝试调用
dismissPopoverAnimated:
时,您是否验证了
popover
不是
nil
?请使用调试器。在该行设置断点。当到达断点时,查看
popover
的值。如果看不到最新的代码,也不知道您实际在做什么,就很难进一步提供帮助。您是否更改了这两种图像选择器委托方法?我收到错误:/Users/Graham/Desktop/Interface/Interface/DetailViewController.m:79:24:从DetailViewController.h中不兼容的类型“DetailViewController*const\u strong”分配给“id”,接口声明应该看起来像@interface DetailViewController:UIViewController。那怎么办?也保留它。设置为@interface DetailViewController:UIViewController我收到错误:/Users/Graham/Desktop/interface/interface/DetailViewController.m:79:24:从DetailViewController.h中不兼容的类型“DetailViewController*const\u strong”分配给“id”,接口声明应该看起来像@interface DetailViewController:UIViewController。那怎么办?也保留它。当我尝试这样做时,请将其设置为@interface DetailViewController:UIViewController,我会收到此错误:我会收到此错误:/Users/Graham/Desktop/interface/interface/DetailViewController.m:79:24:从不兼容的类型“DetailViewController*const\u strong”分配给“id”-User1858163,并且当我尝试这样做时,我会收到此错误错误:我得到错误:/Users/Graham/Desktop/Interface/Interface/DetailViewController.m:79:24:从不兼容的类型“DetailViewController*const\uu strong”分配给“id”-user1858163,并且我在标题中确实有