ios - 将 UIImage 从一个 Viewcontroller 传递到另一个 Viewcontroller 得到 nil

标签 ios objective-c uiimagepickercontroller viewcontroller

我正在尝试将 UIImage 从一个 Viewcontroller 传递到另一个 Viewcontroller,方法与我传递 NSString 的方式相同,但在图像的情况下,它在第二个 Controller 中获得 nil 值。我使用 imagepickercontroller 从第一个 Controller 中的图库中选择图像。 这是 InfoViewcontroller.h-

#import <UIKit/UIKit.h>

@interface InfoViewController : UIViewController

@property(strong,nonatomic) UIImagePickerController *imagepicker;
@property(strong,nonatomic) UIImage *image1;

@end

这是InfoViewcontroller.m-

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

NSData *data=UIImageJPEGRepresentation([info objectForKey:@"UIImagePickerControllerOriginalImage"], 1);
image1=[[UIImage alloc]initWithData:data];
//[imgview1 setImage:image1];
[imagepicker dismissViewControllerAnimated:YES completion:nil];
[self performSegueWithIdentifier:@"tocrop" sender:self];
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{

if ([segue.identifier isEqualToString:@"toimage"]) {

ThirdViewController *destViewController = segue.destinationViewController;
destViewController.image2 =image1;}
}

这是ThirdViewController.h-

#import <UIKit/UIKit.h>

@interface ThirdViewController : UIViewController
@property (strong, nonatomic)  IBOutlet UIImageView *imgview2;
@property (strong,nonatomic) UIImage *image2;
@end

这是ThirdViewController.m-

-(void)viewDidLoad {
[super viewDidLoad];
//if(image2==nil) NSLog(@"abab");
[imgview2 setImage:image2];
}

为什么我得到的是零?

最佳答案

在这里

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

NSData *data=UIImageJPEGRepresentation([info objectForKey:@"UIImagePickerControllerOriginalImage"], 1);
image1=[[UIImage alloc]initWithData:data];
//[imgview1 setImage:image1];
[imagepicker dismissViewControllerAnimated:YES completion:nil];
[self performSegueWithIdentifier:@"tocrop" sender:self];
}

你正在使用“ToCrop”执行 segue,在这里你检查 toimage

if ([segue.identifier isEqualToString:@"toimage"]) 

我认为应该是 if ([segue.identifier isEqualToString:@"tocrop"]) 并且还使用 self.image1=[[UIImage alloc]initWithData:data];

关于ios - 将 UIImage 从一个 Viewcontroller 传递到另一个 Viewcontroller 得到 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32245405/

相关文章:

ios - TestFlight 构建不再与 iPhone 4S 兼容

ios - AFNetworking "Undefined symbols for architecture i386"错误

objective-c - 检查 URL 是否为本地文件?

iphone - Objective-C : why release immediately after creating ui object

ios - iOS 图像 UIButton 中的私有(private)字符串字段

ios Auto-Layout UIView on 3.5 changing height on 4.0 inch

ios - UIGesture 返回 UIImageView 而不是自定义 View

iphone - 使用变量语法打开 url

ios - 如何在 swift 3 中从相机预览 View 裁剪图像?

objective-c - 如何在 UIIMagePickerController 中设置自定义图像