簡體   English   中英

將圖像從UIImagePickerController傳遞到另一個ViewController

[英]Pass image from UIImagePickerController to another ViewController

我正在閱讀很多關於此的帖子,但我無法解決我的問題...我正在嘗試從一個帶有Picker的ViewController發送一個Image到另一個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 - 沒什么......

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

你不應該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;
}

您在imagePickerController中創建了新的PhotoViewController:didFinishPickingMediaWithInfo:但是您不會在視圖層次結構中推送/顯示它,因此它將被解除。 最好的方法是在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
}

[self performSegueWithIdentifier:@"viewPhoto" sender:self]; 將自己創建一個ViewController的新實例。

如果你想要它顯示你的實例你需要用[self presentViewController :viewController animated:YES]或類似的東西來顯示

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

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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM