选择器确实完成采摘不被称为

时间:2014-01-09 14:34:40

标签: ios iphone objective-c uiimagepickercontroller

我有PickerDidFinishPicking的问题,应该在我从我的iphone相机获取图像后调用它但是这里没有调用它是我的代码:

首先是mt h file:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate> 

@end

这是m文件:

#import "CameraOverlay.h"

@interface CameraOverlay ()
@property (nonatomic) UIImagePickerController *imagePickerController;
@end

@implementation CameraOverlay
@synthesize imagePicker;

- (void)viewDidLoad {
    [super viewDidLoad];


   UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    myButton.frame = CGRectMake(0, 0, 200, 60);
    myButton.center = self.view.center;
   [myButton addTarget:self action:@selector(buttonPress:) forControlEvents:UIControlEventTouchUpInside];
   [myButton setTitle:@"Image Picker" forState:UIControlStateNormal];

   [self.view addSubview:myButton];
    [self buttonPress:self];
}

- (void)buttonPress:(id)sender {
    if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {

        // alert the user that the camera can't be accessed
        UIAlertView *noCameraAlert = [[UIAlertView alloc] initWithTitle:@"No Camera" message:@"Unable to access the camera!" delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil, nil];
        [noCameraAlert show];

    } else {



        // prepare imagePicker view
        imagePicker = [[UIImagePickerController alloc] init];
        imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
        imagePicker.delegate = self;
        imagePicker.allowsEditing = NO;
        imagePicker.showsCameraControls = NO;

        // create view for overlay
        CGRect overlayRect = CGRectMake(0, 0, imagePicker.view.frame.size.width, imagePicker.view.frame.size.height);
        UIView *overlayView = [[UIView alloc] initWithFrame:overlayRect];

        // prepare the image to overlay
        //UIImageView *overlayImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"theKing"]];
        //overlayImage.center = overlayView.center;
        //overlayImage.alpha = 0.5;

        // prepare toolbar for overlay
        UIToolbar *overlayToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 600, overlayView.frame.size.width, 40)];
        overlayToolbar.center = CGPointMake(overlayView.center.x, overlayView.frame.size.height - 20);
        overlayToolbar.barStyle = UIBarStyleBlack;

        UIBarButtonItem *takePictureButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:@selector(takePictureButtonPressed:)];
        UIBarButtonItem *flexibleBarSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
        flexibleBarSpace.width = 1000;
        UIBarButtonItem *startStopButton = [[UIBarButtonItem alloc] initWithTitle:@"Snap" style:UIBarButtonItemStyleBordered target:self action:@selector(startStopButtonPressed:)];
        UIBarButtonItem *timedButton = [[UIBarButtonItem alloc]initWithTitle:@"Timed" style:UIBarButtonItemStyleBordered target:self action: @selector(timedButtonPressed:)];
        UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc]initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action: @selector(cancelButtonPressed:)];

        overlayToolbar.items = [NSArray arrayWithObjects:takePictureButton, flexibleBarSpace, startStopButton, timedButton, cancelButton, nil];


       // [overlayView addSubview:overlayImage];
        [overlayView addSubview:overlayToolbar];

        // add the image as the overlay
        imagePicker.cameraOverlayView =overlayView;

        // display imagePicker
        [self.navigationController presentViewController:imagePicker animated:YES completion:nil];
    }
}
-(void)SavePhoto:(UIImage *) imageTosave{
    NSData *pngData = UIImagePNGRepresentation(imageTosave);
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsPath = [paths objectAtIndex:0]; //Get the docs directory
    NSString *dataPath = [documentsPath stringByAppendingPathComponent:@"/CapturesPhotos"];
    NSDate *date = [NSDate date];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    NSTimeZone *zone = [NSTimeZone localTimeZone];
    [formatter setTimeZone:zone];
    [formatter setDateFormat:@"yyyyMMddHHmmss"];
    NSString* namee = [NSString stringWithFormat:@"/IMG_%@.png",[formatter stringFromDate:date]];
    NSString *filePath = [dataPath stringByAppendingPathComponent:namee]; //Add the file name
    [pngData writeToFile:filePath atomically:YES]; //Write the file
}
#pragma mark - UIImagePickerController Delegate Methods
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)editingInfo {

    NSLog(@"imagecontroller");
    // determine if the user selected or took a new photo
    UIImage *selectedImage =  [editingInfo valueForKey:UIImagePickerControllerOriginalImage];

    [self SavePhoto:selectedImage];

    // TODO: Do something with selectedImage (put it in a UIImageView

    // dismiss the imagePicker
    [picker.presentingViewController dismissViewControllerAnimated:YES completion:nil];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - UIBarButton Selectors
- (void)takePictureButtonPressed:(id)sender {
    NSLog(@"takePictureButtonPressed...");
    [self.imagePickerController takePicture];
        // TODO: take picture!
}
- (void)startStopButtonPressed:(id)sender {
    NSLog(@"startStopButtonPressed...");
    // TODO: make this do something
}
- (void)timedButtonPressed:(id)sender {
    NSLog(@"timedButtonPressed...");
    // TODO: implement timer before calling takePictureButtonPressed
}
- (void)cancelButtonPressed:(id)sender {
    NSLog(@"cancelButtonPressed");
    [self.navigationController dismissViewControllerAnimated:NO completion:nil];
    [self.navigationController popViewControllerAnimated:YES];
}
@end

当我按下按钮从相机获取图像时,不会调用选择器功能,为什么会发生这种情况? 我首先按下连接到[self.imagePickerController takePicture]的按钮,然后调用选择器,然后调用方法-(void)SavePhoto:(UIImage *) imageTosave保存图片。

提前致谢

2 个答案:

答案 0 :(得分:0)

确保在类声明中添加了UIImagePickerControllerDelegate类别。

答案 1 :(得分:0)

[self buttonPress:self];中的viewDidLoad按钮太早了。

第一步:更改代码以便进行编译,将所有内容imagePicker更改为self.imagePickerController

第二步:注释掉self buttonPress行。如果您想提前触发选择器,请在viewDidAppear中进行VC转换。