从UIImagePickerController到下一个View Controller通过segue的照片不起作用

时间:2014-05-12 22:36:11

标签: ios objective-c uiimagepickercontroller segue transfer

我无法使用CamViewController中的imagePickerController将拍摄的照片传输到下一个ViewController - CameraViewController - 来自segue。 我尝试在CameraViewController上设置拍摄的图像,但它从不显示。


更新20140519

工作解决方案。我必须将所选照片复制到UImage而不是UIImageView.image。所以我创建了一个新的UIImage属性,而不是它的工作原理。

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

    UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
//   self.imageView.image = chosenImage; This did not work. self.imageView.image was nil
    self.image = chosenImage;

    [picker dismissViewControllerAnimated:YES completion:NULL];
    [self performSegueWithIdentifier:@"toCameraView" sender:info];
}

然后我可以毫无问题地转移它:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    CameraViewController *cvc = [segue destinationViewController];
    // cvc.image = self.imageView.image; This did not work, as it was nil
    cvc.image = self.image;
    DLog(@"%@, cvcimage", cvc.image); 
}

调试输出

[CameraViewController viewDidLoad] [Line 57] <UIImageView: 0x166ee910; frame = (0 0; 320 320); autoresize = W+H; userInteractionEnabled = NO; layer = <CALayer: 0x166ee990>>, imageView

CamViewController.h

#import <UIKit/UIKit.h>
#import "CameraViewController.h"

@interface CamViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate>
@property (strong, nonatomic) IBOutlet UIImageView *imageView;
- (IBAction)takePhoto:(id)sender;
- (IBAction)selectPhoto:(id)sender;
@end

CamViewController.m

#import "CamViewController.h"

@interface CamViewController ()
@end

@implementation CamViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {

        UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Error"
                                                              message:@"Device has no camera"
                                                             delegate:nil
                                                    cancelButtonTitle:@"OK"
                                                    otherButtonTitles: nil];
        [myAlertView show];
    }
}

-(void)viewDidAppear:(BOOL)animated {
    [self takePhoto:nil];  
}

- (IBAction)takePhoto:(id)sender {
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.allowsEditing = YES;
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;

    [self presentViewController:picker animated:YES completion:NULL];
}

- (IBAction)selectPhoto:(id)sender {
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.allowsEditing = YES;
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

    [self presentViewController:picker animated:YES completion:NULL];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
    self.imageView.image = chosenImage;

    [picker dismissViewControllerAnimated:YES completion:NULL];
    [self performSegueWithIdentifier:@"toCameraView" sender:info];
}

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

#pragma mark - Navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    CameraViewController *cvc = [segue destinationViewController];

    // I try to set the photo, but nothing of below worked
    // on the CameraViewController the image is not shown. 

//    cvc.image = self.imageView.image; // did not work 
//    cvc.imageView.image = self.imageView.image; // did not work
//    cvc.imageView.image = [self.imageView.image copy]; // did not work
//    cvc.imageView = self.imageView; // did not work
    cvc.imageView = [self.imageView copy]; // did not work

}

CameraViewController.h

#import <UIKit/UIKit.h>
#import "WebApi.h"

@interface CameraViewController : UIViewController <UINavigationControllerDelegate, UITableViewDelegate, UITableViewDataSource >
@property (strong, nonatomic) IBOutlet UIImageView *imageView;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) WebApi *swebapi;
@property (strong, nonatomic) NSArray *cellRows;
@property (nonatomic, strong) UIImage *image; // try this as well, like stated in http://stackoverflow.com/a/15311031/1933185
-(IBAction)upload:(id)sender;

@end

CameraViewController.m

#import "CameraViewController.h"

@interface CameraViewController ()

@property (nonatomic) int photoIsTaken;

@end

static int cellButtonHeight = 90;

@implementation CameraViewController

- (id)init
{
    self = [super init];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    self.swebapi = [[WebApi alloc] init];
    [super viewDidLoad];
    // this did not work either: 
    // self.imageView.image = self.image;
    DLog(@"%@, imageView", self.imageView); 

    self.cellRows = @[@"myimage:"];
    [self.tableView setDelegate:self];
    [self.tableView setDataSource:self];
}

-(void)viewDidAppear:(BOOL)animated {
    NSIndexPath *placeRow = [NSIndexPath indexPathForItem:0 inSection:0];

    [self.tableView beginUpdates];
    [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:placeRow] withRowAnimation:UITableViewRowAnimationAutomatic];
    [self.tableView endUpdates];
}

- (IBAction)upload:(id)sender {
    [self.swebapi uploadImage:self.imageView.image];
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.cellRows.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"photoMetaData";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    cell.backgroundColor = [UIColor clearColor];

    SEL functionCall = NSSelectorFromString(self.cellRows[indexPath.row]);
    [self performSelector:functionCall withObject:cell];

    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return (indexPath.row == 1 || indexPath.row == 3) ? cellButtonHeight : 50.0;
}

#pragma mark - cell configuration 
-(void)myimage:(UITableViewCell*)cell {
    // do stuff
}

@end

2 个答案:

答案 0 :(得分:0)

执行此操作时:

cvc.imageView = [self.imageView copy];

您正在断开cvc.imageView作为指向故事板或xib中添加的视图的指针。它现在引用您在CamVC中添加的视图的副本。您永远不会将此视图添加到子视图中,因此您永远不会看到它。

我认为你最好这样做:

cvc.imageView.image = self.imageView.image;

答案 1 :(得分:0)

我认为因为CameraViewController尚未加载且self.imageView可能为零。在CameraViewController UIImage中创建一个属性,并在prepareForSegue中设置该属性,只需尝试NSLog self.imageView并检查它是否为零。