i would love to have some help or any keyword that i can use to search for.
My problem is I have one UIView, called “UpdateViewController.xib” that load 20 small images and text below those images by programmatically.
and when user click on those images it will change to next view that i created by IB, called “imageSumVuew.xib” and i have a button to link back to UpdateViewController.
#import "imageSumView.h" // next view that i wanna load//
// the transition to next view
imageSumView *nextView = [[imageSumView alloc]init];
self.modalPresentationStyle = UIModalTransitionStyleCrossDissolve;
[self presentViewController:nextView animated:YES completion:NULL];
[nextView release];
in the nextView i have code similar to this which come back to this view
#import "UpdateViewController.h" // old that i wanna load back//
// the transition to old view
UpdateViewController *oldView = [[UpdateViewController alloc]init];
self.modalPresentationStyle = UIModalTransitionStyleCrossDissolve;
[self presentViewController:oldView animated:YES completion:NULL];
[oldView release];
the problem is when it did load back to UpdateViewController, all my images and text has to reload all over again.
The question is ” how can i keep cache of the UpdateViewController view?“, i don’t want user to reload images all over again because they have to go back and forth between this page for several times to see which image that they wanna pick.
Think of Instragram that you see list of your friends images then you wanna check your first friends’s photo and after that you come back to overall image of your friends without loading and choose second friends.
Store image data with unique id to temporary directory once it is downloaded for the first time. For the next time check for that user id’s image in your directory, if it is there then load image from there. For example :
#define TMP NSTemporaryDirectory() NSString *filename=[NSMutableString stringWithFormat:@"userimage_%@",userId]; NSString *uniquePath = [TMP stringByAppendingPathComponent:filename]; NSData *dataImage = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageUrl]] UIImage *image = [[UIImage alloc] initWithData: dataImage]; [UIImageJPEGRepresentation(image, 1.0f) writeToFile: uniquePath atomically: YES]; [image release];To get ur image you can do something like below:
- (NSData *) getImage: (NSString *)userId { NSString *filename=[NSMutableString stringWithFormat:@"userimage_%@,userId]; NSString *uniquePath = [TMP stringByAppendingPathComponent: filename]; if([[NSFileManager defaultManager] fileExistsAtPath: uniquePath]) { return [[NSData alloc] initWithContentsOfFile:uniquePath]; } return nil; }