Reputation: 229
I have 2 ViewControllers, in 1st button which download image, how can I cache this image and send to second ViewController? Thanks a lot.
First ViewController:
In .h
@interface asdViewController : UIViewController {
IBOutlet UIButton *actBtnGet;
}
@property (strong, nonatomic) IBOutlet UIImageView *ImageView;
@end
In .m
- (IBAction)actBtnGet:(id)sender {
id path = @"http://www.host.com/132.png";
NSURL *url = [NSURL URLWithString:path];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data];
self.ImageView.image=img;
}
Upvotes: 1
Views: 544
Reputation: 11607
Create a property in your 2nd view controller:
// inside your Second View Controller .h file
@property (nonatomic, strong) UIImage *storedImage;
Then before you push your 2nd view controller, you set the storedImage:
// inside your First View Controller .m file
-(void)showSecondVC
{
SecondVC *secondVC = [[SecondVC alloc] init];
secondVC.storedImage = self.downloadedImage;
[self.navigationController pushViewController:secondVC animated:YES];
}
For the caching mechanism, there are already two great libraries that already provides that:
SDWebImage ( https://github.com/rs/SDWebImage )
AFNetworking ( https://github.com/AFNetworking/AFNetworking )
I personally prefer SDWebImage :D
After you have imported one of the library into your project, it's usually a 1 line of code like this:
[myImageView setImageWithURL:imageURL placeholderImage:nil options:0 completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {
// do something here in the completion callback block
[self showSecondVC];
}];
Those two libraries will cache the image if it detects the same URL address being used, so if you give it a URL of:
http://www.imageserver.com/someImage.png
the first time, it downloads and caches that image at that address. The second time you pass it the same URL address again it grabs it from the cache rather than download it from the server.
SDWebImage has more fancy options to download image using a download manager without needing to use a UIImageView if you require it, but that's usually for more complicated scenarios, the above method will suffice majority of the time.
Upvotes: 2
Reputation: 671
You can add a method in your secondViewController.
-(void)initWithImage:(UIImage *)image{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
//assign this image to a UIImage object
}
return self;
}
Call this method from First View Controller
SecondVC *secondVC = [[SecondVC alloc] initWithImage:self.image];
[self.navigationController pushViewController:secondVC animated:YES];
Upvotes: 1
Reputation: 91
I always do it like this (assuming that you use segue in order to go to second view):
In second view controllers .h file create image property:
@property (strong, nonatomic) IBOutlet UIImageView *Image;
In the first view controller use this in your .m file:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
//Use this if statement only if you have several segues coming from your first view, if so, you also need to add identifier to the segue you are using
if([segue.identifier isEqualToString:@"segueToSecondView"])
{
SecondViewController *secondView = segue.destinationViewController;
secondView.imageView.image = img;
}
}
Upvotes: 0
Reputation: 158
What Zhang said (although that can't be called caching) or if you are looking for image cache that would cache images downloaded from web, look into AFNetworking which has a nice category for UIImageView. The passing to another view controller part is OK as described.
Upvotes: 0