似乎有点老但我无法解决这个问题....现在已经忙了很长一段时间了,我找不到一个有用的例子来解决这个问题,所以如果有人可以提供帮助,我将不胜感激,我我只是试图使用图像选择器将图像从视图控制器传递到另一个视图,我有2个视图控制器:viewcontroller和第二个视图,我在这里放置视图控制器h文件的代码:
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController <UINavigationControllerDelegate, UIImagePickerControllerDelegate>
{
UIImagePickerController *picker;
IBOutlet UIImageView *selectedImage;
SecondView *imageView;
}
@property (nonatomic, retain) UIImageView *selectedImage;
@property (nonatomic, retain) SecondView *secondView;
-(IBAction)sendImage:(id)sender;
-(IBAction)openGallery:(id)sender;
@end
这里是视图控制器的m文件:
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize selectedImage;
@synthesize secondView;
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (IBAction)secondView:(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];
}
-(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];
}
objectForKey:UIImagePickerControllerOriginalImage];
-(IBAction)openGallery:(id)sender
{
picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:nil];
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *) Picker
{
[self dismissViewControllerAnimated:YES completion:nil];
[Picker dismissViewControllerAnimated:YES completion:nil];
}
- (void)imagePickerController:(UIImagePickerController *) Picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
selectedImage.image = [info objectForKey:UIImagePickerControllerOriginalImage];
[self.navigationController pushViewController:secondView animated:YES];
[Picker dismissViewControllerAnimated:YES completion:nil];
}
@end
这是secondview的h文件:
#import <UIKit/UIKit.h>
@interface SecondView : UIViewController
{
UIImageView *imgView;
UIImage *theImage;
}
@property (nonatomic, retain) IBOutlet UIImageView *imgView;
@property (nonatomic, retain) UIImage *theImage;
-(void)setImage:(UIImage *)image;
@property (nonatomic, readwrite) IBOutlet UIImageView *imageView;
@end
这是第二个视图的m文件:
#import "SecondView.h"
@interface SecondView ()
@end
@implementation SecondView : UIViewController
@synthesize imgView;
@synthesize theImage;
-(void)setImage:(UIImage *)image
{
[imgView setImage:image];
}
- (void)viewDidLoad
{
[super viewDidLoad];
[_imageView setImage:theImage];
}
- (void)loadView {
self.title = @"Photo";
_imageView = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
self.imageView.contentMode = UIViewContentModeScaleAspectFit;
self.imageView.backgroundColor = [UIColor blackColor];
self.view = self.imageView;
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[_imageView setImage:theImage];
}
@end
我只是继续在第二个视图上看到一个空白的屏幕....任何帮助非常感谢....提前感谢....
答案 0 :(得分:0)
如果在第二个viewcontroller上使用segue附加了按钮,请使用此代码。
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
SecondView *secView = segue.destinationViewController;
self.secondView = secView;
secView.theImage = selectedImage.image;
}
答案 1 :(得分:0)
您可以在其他UIImageView
上使用新的view controller
,并使用prepareForSegue
方法传递此图片。
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([segue.identifier isEqualToString:@"editphoto"])
{
YourNextViewController*editImageView=segue.destinationViewController;
UIImage*clickedImage=capturedPicImageView.image;
editImageView.passedImage=clickedImage;
}
}
在YourNextViewController
创建UIImage*clickedImage
并向其提供属性。并且不要忘记在viewDidLoad
分配。希望这会有所帮助。