我正在使用popover显示相机胶卷,用户可以选择一张照片并将其显示在UIImageView
中。
下面的代码将显示选择器,但点击图片后我会得到一个NSexception。我在这里看了类似的问题,谷歌并没有找到解决方案。我的popover很强大。任何帮助将不胜感激
#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
答案 0 :(得分:0)
您的问题是您在不首先解除弹出窗口的情况下解雇视图。
您可以确保将其排除在覆盖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){
你有错误的地方。