我不能通过segue将CamViewController
中的imagePickerController传输到下一个ViewController - CameraViewController
。我尝试在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
当你这样做的时候:
cvc.imageView = [self.imageView copy];
你正在断开cvc.imageView
作为一个指针的视图添加在故事板或xib。它现在引用您在CamVC
中添加的视图的副本。你永远不会把这个视图添加到子视图,所以你永远不会看到它。
我认为你最好这样做:
cvc.imageView.image = self.imageView.image;
我想是因为CameraViewController
还没有加载,self.imageView
可能是nil。在CameraViewController
中创建UIImage
的属性,并在prepareForSegue
中设置,只是尝试NSLog self.imageView
并检查它是否为nil