User User - 4 months ago 44
Objective-C Question

How to download image and show?

Use this code for downloading background button image. The image will appear after loading but in the next session disappears.

- (void)viewDidLoad {
[super viewDidLoad];


_button = [UIButton buttonWithType:UIButtonTypeCustom];
[_button setTitle:@"Show View" forState:UIControlStateNormal];
[_button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal ];
_button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[_button setBackgroundImage: image forState:UIControlStateNormal];
[self.view addSubview:_button];
session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:[NSOperationQueue mainQueue]];
[self.progressView setProgress:0 animated:NO];

}

- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask
didFinishDownloadingToURL:(NSURL *)location;{
NSLog(@"%@", location);

data = [NSData dataWithContentsOfURL:location];

image = [UIImage imageWithData:data];

[_button setBackgroundImage: image forState:UIControlStateNormal];
}


- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask
didWriteData:(int64_t)bytesWritten
totalBytesWritten:(int64_t)totalBytesWritten
totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite;{

[self.progressView setProgress:totalBytesWritten/totalBytesExpectedToWrite animated:YES];

}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (IBAction)playAction:(id)sender {
if(downloadTask == nil){

NSURL *url =[NSURL URLWithString:@"http://www.freeiconspng.com/uploads/ios-png-6.png"];
downloadTask = [session downloadTaskWithURL:url];
[downloadTask resume];

}

else

[downloadTask resume];
}


————————————————————————————————————————————
How to download image once and load in every session?

Answer

You need save images in cache.

Better way for this is to use FrameWork SDWebImage. It will done all job for you automatically.

Import this file in your class:

#import <SDWebImage/UIButton+WebCache.h>

Use any of this method:

- (void)sd_setBackgroundImageWithURL:(NSURL *)url forState:(UIControlState)state;
- (void)sd_setBackgroundImageWithURL:(NSURL *)url forState:(UIControlState)state placeholderImage:(UIImage *)placeholder;
- (void)sd_setBackgroundImageWithURL:(NSURL *)url forState:(UIControlState)state placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options;
- (void)sd_setBackgroundImageWithURL:(NSURL *)url forState:(UIControlState)state completed:(SDWebImageCompletionBlock)completedBlock;
- (void)sd_setBackgroundImageWithURL:(NSURL *)url forState:(UIControlState)state placeholderImage:(UIImage *)placeholder completed:(SDWebImageCompletionBlock)completedBlock;
- (void)sd_setBackgroundImageWithURL:(NSURL *)url forState:(UIControlState)state placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options completed:(SDWebImageCompletionBlock)completedBlock;
Comments