Reputation: 93
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];
Upvotes: 0
Views: 196
Reputation: 3721
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.
Upvotes: 0
Reputation: 452
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.
Upvotes: 1