通过segue移动图像以加载到另一个UIImageView



我目前正在构建一个iOS应用程序。我试图保存用户当前正在查看的内容,以便我可以为下一个视图加载该内容。我知道如何通过

保存图像
 CGSize size = [self.tableView bounds].size;
        UIGraphicsBeginImageContext(size);
        [[self.tableView layer] renderInContext:UIGraphicsGetCurrentContext()];
        UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();

然而,我不知道如何将它们移动到下一个视图控制器。由于某些原因,上面的代码拒绝在下一个视图控制器的图像视图中加载它。

_incommingImage是一个图像视图。

我这样保存它。

 NSData *imageData = UIImageJPEGRepresentation(newImage, 1.0);
        NSArray *pathArr = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                               NSUserDomainMask,
                                                               YES);
        NSString *path = [[pathArr objectAtIndex:0]
                          stringByAppendingPathComponent:@"img.data" ];
[imageData writeToFile:path atomically:YES];

我用这个装载它。

 NSArray *pathArr = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                           NSUserDomainMask,
                                                           YES);
    NSString *path = [[pathArr objectAtIndex:0]
                      stringByAppendingPathComponent:@"img.data" ];
    NSData *retrievedData = [NSData dataWithContentsOfFile:path];
    _incommingImage.image = [UIImage imageWithData:retrievedData];
       [_incommingImage release];

我同意Robert的看法。

FirstViewController.m:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"Your_Identifier"]) {
        SecondViewController *secondVC = segue.destinationViewController;
        secondVC.incommingImage = myImage;
    }
}

SecondViewController.h:

@property (strong, nonatomic) UIImage *incommingImage;

一旦你的SecondViewController出现,图像将自动设置为incommingImage。确保在storyboard中设置segue的标识符匹配prepareForSegue。

我在photoApp项目中做了类似的事情,我有一个视图来显示图像。

1。让我们在nextView中设置UIImage属性。

NextView.h
@interface
@property (nonatomic, strong) UIImage * image;
@end

2。从属性设置图像。这实际上可以在segue上完成。从代码来看,我不确定segue是如何发生的,但这里是设置segue的代码它也设置了背景图像。

NextView *nextView = [[NextView alloc]init];
nextView.image = 
//the image that you want. 
// I don't know how you are getting this image so 
//you may have to have other code now to get this image from its picker method. 

3。在下一个视图中你也需要有一个UIImageView。imageView需要在后台,因此所有在后台显示的东西都可以是这个UIImageView的子视图。

- (void)viewDidLoad
{
    [super viewDidLoad];
    UIimageView *imageView = [[UIImageView alloc]initWithImage:image];
    imageView.userInteractionEnabled=YES
    //if you need to add say a button on the view
    //your going to have to configure the button's dimensions this is a template. 
    UIButton *buttonView = [UIButton alloc]initWithFramce:CGRectMake(X,Y,Width,Height)];
    [imageView addSubview:buttonView];
}

编辑:让我知道如果你正在使用故事板,在这种情况下,我可以调整这段代码与故事板更顺畅的过渡。

最新更新