0

到目前为止,这是我的代码,它允许我从照片库中选择一张照片并将其显示在一个控制器的图像视图中,视图控制器有一个按钮,该按钮有一个将其发送到此视图中的下一个视图控制器的 segue控制器是另一个图像视图。

我希望第一个控制器中的图像视图显示在第二个视图控制器的第二个图像视图中,这是我到目前为止的代码。

我不知道要采取什么步骤来实现这一点,所以在采取的步骤中的任何帮助将不胜感激,我尝试了各种方法,但我认为我做得不太对,所以我决定给它一个干净的石板并得到一些建议

视图控制器.h

#import <UIKit/UIKit.h>


@interface ViewController : UIViewController <UIImagePickerControllerDelegate,  UINavigationControllerDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@property (strong, nonatomic) UIImage *chosenImage;

@property(strong, nonatomic) UIImagePickerController *imagePicker;


@end

视图控制器.m

#import "ViewController.h"


@interface ViewController ()

@end


@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSString *myGrabbedImage = @"myGrabbedImage.png";
    NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,          NSUserDomainMask, YES);
    NSString *documentDirectory = [path objectAtIndex:0];
    NSString *fullPath = [documentDirectory stringByAppendingPathComponent:myGrabbedImage];
    NSData *data = [NSData dataWithContentsOfFile:fullPath];
    [[self imageView]setImage:[UIImage imageWithData:data]];
}
- (IBAction)chooseImage:(id)sender
{
    self.imagePicker = [[UIImagePickerController alloc]init];
    self.imagePicker.delegate = self;
    [self.imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
    [self presentViewController:self.imagePicker animated:YES completion:nil];
}

-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    self.chosenImage = info[UIImagePickerControllerOriginalImage];

    NSData *data = UIImagePNGRepresentation(self.chosenImage);
    NSString *myGrabbedImage = @"myGrabbedImage.png";
    NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentDirectory = [path objectAtIndex:0];

    [self.imageView setImage:self.chosenImage];
    [self dismissViewControllerAnimated:YES completion:nil];
    NSString *fullPathToFile = [documentDirectory stringByAppendingPathComponent:myGrabbedImage];
    [data writeToFile:fullPathToFile atomically:YES];


}


- (void) imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];

}




@end
4

1 回答 1

0

在您的第二个 ViewController 上创建一个UIImage属性并通过prepareForSegue

(不要忘记导入您的第二个 ViewController 以便ViewController了解它)

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{

    if ([[segue identifier] isEqualToString:@"yourSegueIdentifier"]) {
        yourSecondViewController *secondVC = [segue destinationViewController];
        secondVC.nextImage = self.chosenImage;
    }

}

然后在你的第二个 ViewController 中你可以设置 imageView

yourSecondImageView.image = nextImage;
于 2013-08-12T18:05:15.160 回答