我正在从照片库获取用户的图像,并希望将其传递给另一个视图...
我已经了解到我应该能够简单地执行以下操作:
将我的第二个视图中的UIImageView设置为来自我的第一个视图中的UIImageView的图像... 但是第二个视图中的UIImageView为空...
我的代码如下...
ViewController.h:
在另一篇帖子中,我读到另一种方法是这样做的:
然后在 secondview.h 文件中。
但是这两种方法都导致UIImageView为空... 那么正确的方法是什么呢?
提前感谢!
编辑
目前SecondView非常简单...(在发布的代码中,我尝试遵循第二种方法)
在第二个视图中,我创建了UIImage *theImage - 然后在viewDidLoad中设置了[imgView setImage:theImage]; 这个方法成功了...
我已经了解到我应该能够简单地执行以下操作:
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;
and .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.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end
解决方案
好的...回到了第一种方法并做了一些修改,最终解决了问题...-(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];
}
在第二个视图中,我创建了UIImage *theImage - 然后在viewDidLoad中设置了[imgView setImage:theImage]; 这个方法成功了...