0

I am currently building an application for iOS. I am trying to save what the user was currently looking at so that I can load that up for the next view. I know how to save images via

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

However I do not know how to move them to the next view controller. For some reason, the above refuses to load it up in an image view within the next view controller.

Solved

_incommingImage is an image view.

I save it like this.

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

I load it up then with this.

 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];
user3642059
  • 93
  • 1
  • 6

2 Answers2

1

I agree with 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;

Once your SecondViewController appears, the image will automatically be set to incommingImage. Be sure to set the identifier of your segue on the storyboard to match the identifier under prepareForSegue.

Jeremy H
  • 452
  • 7
  • 20
  • The setter for the image in your FirstViewController is myImage. Simply replace myImage with the image from FirstViewController. – Jeremy H Jul 09 '14 at 20:00
  • In case you are additionally asking how to render the first view to an image, have a look at http://stackoverflow.com/a/7965126/3659846 – robert Jul 09 '14 at 20:29
  • Unfortunately, I was not able to get either solution to transfer the image. I did get the image properly screenshoted and I have updated the question with it. – user3642059 Jul 09 '14 at 22:04
0

I did something similar in a photoApp project where I had a view to display an image.

1.lets go ahead and setup that UIImage property in our nextView.

NextView.h

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

2.Set the image from the property. This can actually be done upon a segue. From looking at your code, I'm not sure how the segue occurs, but here is code to setup a segue that sets the background image as well.

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.Within the next view you are going to have to have a UIImageView in it as well. The imageView is going to need to be in the background, so everything that will display in it can be a subview of this 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];

}

EDIT: Let me know if you are using storyboard in that case I can tweak this code for a smoother transition with Storyboard.

TheM00s3
  • 3,677
  • 4
  • 31
  • 65
  • Take a look @jeremyHerrero's answer below and use the ``prepareForSegue:sender:`` method for step 2. Step 3 of my answer is how you can setup the 2ndViewController's image. If you have any questions on that part let me know. – TheM00s3 Jul 09 '14 at 20:07
  • 1
    In step 3 you are missing to add the imageView. If you suggest to add every other view to the imageView, you should set `imageView.userInteractionEnabled=YES` since it is disabled per default for `UIImageView` – robert Jul 09 '14 at 20:40
  • Unfortunately, I was not able to get either solution to transfer the image. I did get the image properly screenshoted and I have updated the question with it. – user3642059 Jul 09 '14 at 22:01