Image Downloading show in ProgressView

NSURL, UIImageView, UIImage, UIProgressView, NSURLSession

ViewController.h

<NSURLSessionDelegate, NSURLSessionDownloadDelegate>

@property (weak, nonatomic) IBOutlet UIImageView *imageView;

@property (weak, nonatomic) IBOutlet UIProgressView *progressView;

ViewController.m

– (void)viewDidLoad {

    [super viewDidLoad];

    

    NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];

    NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfiguration delegate:self delegateQueue:nil];

    NSURLSessionDownloadTask *downloadTask = [session downloadTaskWithURL:[NSURL URLWithString:@”https://i.ytimg.com/vi/QGiJFumHUPo/maxresdefault.jpg&#8221;]];

    [downloadTask resume];

    

    

    // Do any additional setup after loading the view, typically from a nib.

}

– (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location {

    NSData *data = [NSData dataWithContentsOfURL:location];

    

    dispatch_async(dispatch_get_main_queue(), ^{

        //[self.progressView setHidden:YES];

        [self.imageView setImage:[UIImage imageWithData:data]];

    });

}

– (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didResumeAtOffset:(int64_t)fileOffset expectedTotalBytes:(int64_t)expectedTotalBytes {

    

}

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

    float progress = (double)totalBytesWritten / (double)totalBytesExpectedToWrite;

    

    dispatch_async(dispatch_get_main_queue(), ^{

        [self.progressView setProgress:progress];

    });

}

Click here to download Image Downloading show in ProgressView Project

Leave a comment