2014-04-02 29 views
0

我正在阅读很多关于此的帖子,但我无法解决我的问题... 我试图发送一个图像从一个ViewController与选择器...到另一个ViewController但图像没有出现......从UIImagePickerController传递图像到另一个ViewController

我有2个VC:

HomeViewController.h:

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

@interface QuizTypeViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate> 

- (IBAction)photo:(id)sender; 

@end 

HomeViewController.m(我得到正确的图像,我要去发布只是SEGUE代码)

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

UIImage *chosenImage = info[UIImagePickerControllerEditedImage]; 

PhotoViewController *controller = [PhotoViewController new]; 
controller.imageView.image = chosenImage; 

[picker dismissViewControllerAnimated:YES completion:NULL]; 
[self performSegueWithIdentifier:@"viewPhoto" sender:self]; 

} 

PhotoViewController.h

#import <UIKit/UIKit.h> 

@interface PhotoViewController : UIViewController 
@property (strong, nonatomic) IBOutlet UIImageView *imageView; 
@end 

PhotoViewController.m - 没什么......

我在做什么错了?我不知道...

+0

你有没有合成的ImageView的? – Ramdy

+0

@Ramdy在带有LLVM的Xcode 5中,不需要合成等等。 – rckoenes

+0

那么,我们不需要使用合成?@rckoenes – Ramdy

回答

1

你不应该new一个PhotoViewController。当你调用

[self performSegueWithIdentifier:@"viewPhoto" sender:self];

一个PhotoViewController实例将自动为您创建。你应该做的是将选定的图像传递给它。并在你的PhotoViewController的某些方法(例如:viewDidLoad)中显示它。

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    PhotoViewController *photoViewController = segue.destinationViewController; 
    photoViewController.image = self.chosenImage; 
} 
+1

中添加'@ synthesize',我必须插入:“self.imageView.image = self.image;” – pekpon

0

[self performSegueWithIdentifier:@"viewPhoto" sender:self];将自己创建一个新的ViewController实例。

如果你想让它显示你的情况下,你需要[self presentViewController :viewController animated:YES]或类似

1

您在imagePickerController创建新PhotoViewController显示:didFinishPickingMediaWithInfo:但你不考虑层次推动/目前它,所以它会被驳回。最好的方式是如在performSegueWithIdentifier参数通图像:发送方法:

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

UIImage *chosenImage = info[UIImagePickerControllerEditedImage]; 
_tmp = chosenImage; 

[picker dismissViewControllerAnimated:YES completion:NULL]; 
[self performSegueWithIdentifier:@"viewPhoto" sender: chosenImage]; 

} 

和在prepareForSegue:赛格瑞:方法从发送端获得的图像并传递给目的地视图控制器:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    // TODO: check segue identifier 
    PhotoViewController *vc = (PhotoViewController*)segue.destinationViewController; 
    // Get the image 
    UIImage *img = (UIImage*)sender 
    // Pass image to the new view controller. 
    vc.imageView.image = img; 
    //It can failed because your image view can not be created 
    // You should use @property for UIImage, pass img to image and in view did load 
    //assign imageView.image = image 
} 
0
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{ 

    UIImage *chosenImage = info[UIImagePickerControllerEditedImage]; 
_tmp = chosenImage; 

    PhotoViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"viewPhoto"]; 
controller.imageView.image = chosenImage; 

[picker dismissViewControllerAnimated:YES completion:NULL]; 
[self presentViewController:controlle animated:YES completion:nil]; 
} 
0

HomeViewController.m

PhotoViewController *controller = [PhotoViewController new]; 
controller.image = chosenImage; 

PhotoViewController.h

@property (weak, nonatomic) IBOutlet UIImage *image; 
@property (strong, nonatomic) IBOutlet UIImageView *imageView; 

PhotoViewController.m

imageView.image = image; 
相关问题