【问题标题】:Pass image from UIImagePickerController to another ViewController将图像从 UIImagePickerController 传递到另一个 ViewController
【发布时间】:2014-04-02 09:20:42
【问题描述】:

我正在阅读很多关于此的帖子,但我无法解决我的问题... 我正在尝试使用 Picker 将图像从一个 ViewController 发送到另一个 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 - 没什么...

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

【问题讨论】:

  • 你合成了imageview吗?
  • @Ramdy 在带有LLVM 的 Xcode 5 中,不需要合成等等。
  • 那么,我们不需要使用 synthesize 吗? @rckoenes
  • @Ramdy 不,如果您使用 LLVM,现在是强制性的。您不再需要在.m 中添加@synthesize

标签: ios objective-c cocoa-touch uiimagepickercontroller


【解决方案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;
}

【讨论】:

  • 在 viewDidLoad 我必须插入这个:“self.imageView.image = self.image;”
【解决方案2】:

您在 imagePickerController:didFinishPickingMediaWithInfo: 中创建了新的 PhotoViewController,但您没有在视图层次结构中推送/呈现它,因此它将被关闭。最好的方法是在 performSegueWithIdentifier:sender 方法中将图像作为参数传递:

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

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

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

}

并在 prepareForSegue:segue: 方法中从发送者获取图像并将其传递给目标视图控制器:

- (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
}

【讨论】:

    【解决方案3】:

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

    如果您希望它显示您的实例,您需要使用 [self presentViewController :viewController animated:YES] 或类似名称显示

    【讨论】:

      【解决方案4】:
       - (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];
      }
      

      【讨论】:

        【解决方案5】:

        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;
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-07-25
          相关资源
          最近更新 更多