gpt4 book ai didi

ios - 将图像从一个 View 传递到另一个 View

转载 作者:可可西里 更新时间:2023-11-01 04:31:39 26 4
gpt4 key购买 nike

我从照片库中获取用户的图像,我想将它传递给另一个 View ...

我读过我应该能够简单地做这样的事情:

secView.imgView.image = selectedImage.image;

在第二个 View 上将我的 UIImageView 设置为来 self 的 UIImageView 的图像...但是第二个 View 中的 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.
// 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];
}

在第二个 View 中,我创建了 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];
}

第二 View :

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

关于ios - 将图像从一个 View 传递到另一个 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9496652/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com