将图像从一个视图传递到另一个视图
我正在从照片库中获取用户的图片,我想将其传递给另一个视图…
我读过,我应该可以简单地做这样的事情:
secView.imgView.image = selectedImage.image;
设置我的UIImageView在第二个视图是从我的UIImageView图像…但在第二个视图中的UIImageView只是空的…
我的代码是这个…
ViewController.h:
@interface ViewController : UIViewController <UINavigationControllerDelegate, UIImagePickerControllerDelegate> { UIImagePickerController *picker; IBOutlet UIImageView *selectedImage; SecondView *secondView; } @property (nonatomic, retain) UIImageView *selectedImage; @property (nonatomic, retain) SecondView *secondView; -(IBAction)sendImage:(id)sender; -(IBAction)openGallery:(id)sender; @end
ViewController.m(只有相关的部分)
-(IBAction)sendImage:(id)sender { if(self.secondView == nil) { SecondView *secView = [[SecondView alloc] initWithNibName:@"SecondView" bundle:[NSBundle mainBundle]]; self.secondView = secView; secView.imgView.image = selectedImage.image; } [self.navigationController pushViewController:secondView animated:YES]; } -(IBAction)openGallery:(id)sender { picker = [[UIImagePickerController alloc] init]; picker.delegate = self; picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; [self presentModalViewController:picker animated:YES]; } -(void)imagePickerControllerDidCancel:(UIImagePickerController *) Picker { [Picker dismissModalViewControllerAnimated:YES]; } - (void)imagePickerController:(UIImagePickerController *) Picker didFinishPickingMediaWithInfo:(NSDictionary *)info { // Save the image selectedImage.image = [info objectForKey:UIImagePickerControllerOriginalImage]; [Picker dismissModalViewControllerAnimated:YES]; }
在另一篇文章中,我读到另一种方法是这样做的:
-(IBAction)sendImage:(id)sender { if(self.secondView == nil) { SecondView *secView = [[SecondView alloc] initWithNibName:@"SecondView" bundle:[NSBundle mainBundle]]; self.secondView = secView; [secView setImage:selectedImage.image]; } [self.navigationController pushViewController:secondView animated:YES]; }
然后在secondview.h中
-(void)setImage:(UIImage *)image;
和.m
-(void)setImage:(UIImage *)image { [imgView setImage:image]; }
但是这两种方法都会导致一个空的UIImageView …那么这样做的正确方法是什么?
提前致谢!
编辑
SecondView目前相当简单…(在发布的代码中,我尝试并遵循方法二)
@interface SecondView : UIViewController { UIImageView *imgView; } @property (nonatomic, retain) IBOutlet UIImageView *imgView; -(void)setImage:(UIImage *)image; @end @implementation SecondView @synthesize imgView; -(void)setImage:(UIImage *)image { [imgView setImage:image]; } - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)didReceiveMemoryWarning { // Releases the view if it doesn't have a superview. [super didReceiveMemoryWarning]; // Release any cached data, images, etc that aren't in use. } #pragma mark - View lifecycle - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view from its nib. } - (void)viewDidUnload { [super viewDidUnload]; // Release any retained subviews of the main view. // eg self.myOutlet = nil; } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { // Return YES for supported orientations return (interfaceOrientation == UIInterfaceOrientationPortrait); } @end
解决scheme好的…回到第一种方法,并做了一些最终工作的改变…
-(IBAction)sendImage:(id)sender { if(self.secondView == nil) { SecondView *secView = [[SecondView alloc] initWithNibName:@"SecondView" bundle:[NSBundle mainBundle]]; self.secondView = secView; secView.theImage = selectedImage.image; } [self.navigationController pushViewController:secondView animated:YES]; }
而在第二个视图中,我创build了UIImage * theImage – 然后我设置了[imgView setImage:theImage]; 在viewDidLoad,哪些工作…
这解决了这个问题:
-(IBAction)sendImage:(id)sender { if(self.secondView == nil) { SecondView *secView = [[SecondView alloc] initWithNibName:@"SecondView" bundle:[NSBundle mainBundle]]; self.secondView = secView; secView.theImage = selectedImage.image; } [self.navigationController pushViewController:secondView animated:YES]; }
SecondView:
@interface SecondView : UIViewController { UIImageView *imgView; UIImage *theImage; } @property (nonatomic, retain) IBOutlet UIImageView *imgView; @property (nonatomic, retain) UIImage *theImage; -(void)setImage:(UIImage *)image; @end
并在secondView的.m中:
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view from its nib. [imgView setImage:theImage]; }
我想我发现了这个问题:
在你的类secondView中,你的UIImageView没有连接到nib文件中的对象UIImageView。
你应该有这样的东西:
IBOutlet UIImageView *imgView;
要检查视图是否正确连接到笔尖,您可以这样做:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization [imgView setBackgroundColor:[UIColor redColor]]; } return self; }
你的问题是有趣的,请让我知道,如果它不工作,我会更深入地检查。
//第一个ViewController: – (void)passimage {
Second_ViewController *select_main =[[UIStoryboard storyboardWithName:@"Main" bundle:nil]instantiateViewControllerWithIdentifier:@"Second_ViewController"]; select_main.tt_image=_bg_imageview.image; [[self navigationController] pushViewController:select_main animated:YES];
}
//第二个ViewController传递Uiimage对象:
-
(void)viewDidLoad {
_bg_imageview.image = _bb_image; }
- iOS:Swiftly Slider
- iOS:可以在应用程序文档目录中存储RSA私钥(用于解密文本)吗?
- UIViewControllerHierarchyInconsistency和QiuckTimePlugin
- 如何实现使用多个ViewController的UIPageViewController
- AVCaptureMovieFileOutput后用AVPlayer播放video
- 在导航栏中添加UIView
- APPDelegate中的openURL转换错误NSString – > String(Swift和iOS8)
- 改变inputAccessoryView问题的高度
- 触摸表视图单元格后的第一个方法didSelectRowAtIndexPath还是prepareforSegue?