我從照片庫的用戶得到一個形象,我想將它傳遞到另一個視圖...從一個視圖圖像傳遞到另一個
我讀過,我應該能夠簡單地做這樣的事情:
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當時是相當簡單的...(在發佈代碼中,我嘗試並按照方法2)
@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
SOLUTION 好了...回去到第一種方法,並做了一些最終工作的改變......
-(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 - 然後我設置了[imgView setImage:theImage];在viewDidLoad中,它的工作...
請把SecondView – Ganzolo 2012-02-29 09:45:25
它已經現在添加類的代碼:)不過我已經顯示在代碼的唯一另外除了標準...(請注意,secondView貼是我嘗試提到的第二種方法...) – user969043 2012-02-29 09:49:10
謝謝!將UIImage與UIImageView從原始視圖控制器傳遞給secondViewController也適用於我。也許是因爲第二個VC上的UIImageView是IBOutlet,它不起作用。 – LevinsonTechnologies 2014-11-23 03:52:11