【问题标题】:UIpopoverController dealloc reached while popover is still visible in Ipad在 Ipad 中仍然可见 popover 时达到 UIpopoverController dealloc
【发布时间】:2014-10-03 21:36:45
【问题描述】:

我正在使用弹出框显示相机胶卷,用户可以选择一张图片并将其显示在UIImageView 中。

下面的代码将显示选择器,但在单击图片时,我得到一个 NSexception。我在这里查看了类似的问题,谷歌并没有找到解决方案。我的弹出框设置为强。任何帮助将不胜感激

#import <UIKit/UIKit.h>
#import <CoreData/CoreData.h>
#import <Foundation/Foundation.h>
#import "Pilot.h"

@interface PilotViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate,UIPopoverControllerDelegate>




@property (readonly, nonatomic) UIImage                     *viewImage;

@property (weak, nonatomic    ) IBOutlet UIImageView        *imageView;

@property (nonatomic, strong  ) UIPopoverController         *popoverController;
@property (strong, retain     ) UIImagePickerController     *imagePicker;

//PilotViewController.m

#import "PilotViewController.h"

@interface PilotViewController ()


@end

@implementation PilotViewController
@synthesize popoverController;
@synthesize imagePicker;
@synthesize imageView;






//Button to open library
- (IBAction)library:(id)sender{

    //Create image picker controller

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

    //Set source to the photo library
    self.imagePicker.delegate       = self;
   self. imagePicker.sourceType     = UIImagePickerControllerSourceTypePhotoLibrary;
    self.imagePicker.allowsEditing  = NO;

    self.popoverController          = [[UIPopoverController alloc]
                                       initWithContentViewController:imagePicker];
    self.popoverController.delegate = self;
    CGRect popoverRect = [self.view convertRect:[self.view frame]
                                       fromView:[self.view superview]];

    popoverRect.size.width = MIN(popoverRect.size.width, 80) ;
    popoverRect.origin.x  = popoverRect.origin.x+150;

    [self.popoverController
     presentPopoverFromRect:popoverRect
     inView:self.view
     permittedArrowDirections:UIPopoverArrowDirectionLeft
     animated:YES];
}



-(void) imagePickerController:(UIImagePickerController *)picker didfinishPickingImage:(UIImage *)image
                  editingInfo: (NSDictionary *) editinginfo
{
    imageView.image                = image;
    [self dismissViewControllerAnimated:YES completion:nil];
}






- (IBAction)camera:(id)sender{


    if ([UIImagePickerController isSourceTypeAvailable:
         UIImagePickerControllerSourceTypeCamera])

    {

     //Create image picker controller

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

    self.imagePicker.delegate       = self;
    self.imagePicker.sourceType=
    UIImagePickerControllerSourceTypeCamera;
    self.imagePicker.allowsEditing  = NO;

    self.imagePicker.cameraDevice =
    UIImagePickerControllerCameraDeviceRear;
    [self presentViewController:imagePicker animated:YES completion:nil];
}

    else
    {
    UIAlertView *alert      = [[UIAlertView alloc]
                              initWithTitle:@"Camera failed to open"
                              message:@"Camera is not available"
                              delegate:nil
                              cancelButtonTitle:@"OK"
                              otherButtonTitles: nil];
        [alert show];
    }




}

#pragma mark Image Picker Delegate Methods

//on cancel dimiss picker controller

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


}

//Used when user has chosen an image

- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    UIImage * image                 = info[UIImagePickerControllerOriginalImage];
    imageView.image                = image;
    [self dismissViewControllerAnimated:YES completion:nil];
}



    @end





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

    if ([picker sourceType == UIImagePickerControllerSourceTypeCamera]){
        UIImage * image                 = info[UIImagePickerControllerOriginalImage];
        imageView.image                 = image;
        [self dismissViewControllerAnimated:YES completion:nil];


    }else{


   UIImage * image                 = info[UIImagePickerControllerOriginalImage];
   imageView.image                 = image;
}

          }

    @end

【问题讨论】:

  • 哪一行给出了异常。此外,图像选择器的属性声明看起来不正确 - strong 和 retain 本质上是相同的。
  • 它没有显示一行,只是崩溃时的通用 main.m 文件。是的,这是一个错误,我已经将它分配给非原子,强并且仍然得到相同的 nsexception
  • @Paulw11 您还有其他建议吗?
  • 在断点导航器中添加异常断点
  • 通用的?还是我应该为此采取具体行动

标签: ios ipad uiimagepickercontroller uipopovercontroller popover


【解决方案1】:

您的问题是您在没有先关闭弹出框的情况下关闭了视图。

你可以确保它被忽略覆盖viewWillDisappear

-(void) viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    if (self.popoverController != nil) {
        [self.popoverController dismissPopoverAnimated:animated];
        self.popoverController=nil;
    }
 }

你还应该添加 UIPopoverControllerDelegate 方法popoverControllerDidDismissPopover: -

- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController {
    self.popoverController=nil;
}

更新

didFinishPickingMediaWithInfo 中的 if 语句应该是

if ([picker sourceType] == UIImagePickerControllerSourceTypeCamera){

if (picker.sourceType == UIImagePickerControllerSourceTypeCamera){

你把 ] 放错地方了。

【讨论】:

  • 成功了!一个快速的后续问题,当它关闭弹出框时,它会转到上一个屏幕,我应该更改什么以使其保持在同一个视图控制器上,一旦这样做,所选图像就会显示在 UIImage 点中。再次感谢您的帮助!
  • 我已经尝试了以下代码,它可以工作,但我不能为这两种方法声明两次,我想知道如何实现它。 - (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { UIImage * image = info[UIImagePickerControllerOriginalImage]; imageView.image = 图像;
  • 它会转到上一个屏幕,因为您正在呼叫[self dismissViewControllerAnimated:YES completion:nil]; - 也许您打算呼叫self.popoverController dismissPopoverAnimated: - 我想知道这一点。我不确定我是否理解您之前的评论。您是在问如何让一个班级充当两个 UIImagePickerControllers 的代表?
  • 谢谢,是的,我希望从拍摄的图片或图书馆中更新 uiimage。
  • 您可以在委托中锻炼您正在处理的选择器 - 例如检查标签属性 - 或者创建一个 UIPopoverController 子类来驱动您的弹出框并使其成为 ImagePicker 的委托。它将需要将选定的信息传递回主类 - 您可以通过实现 popoverControllerShouldDismissPopover 委托方法并检查子类的属性来做到这一点
猜你喜欢
  • 1970-01-01
  • 2014-09-24
  • 1970-01-01
  • 1970-01-01
  • 2014-07-16
  • 1970-01-01
  • 1970-01-01
  • 2023-03-28
  • 1970-01-01
相关资源
最近更新 更多