我无法使用CamViewController
中的imagePickerController将拍摄的照片传输到下一个ViewController - CameraViewController
- 来自segue。
我尝试在CameraViewController上设置拍摄的图像,但它从不显示。
工作解决方案。我必须将所选照片复制到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
#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
#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
}
#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
#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
答案 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
并检查它是否为零。