Reputation: 16820
I want to show progress indicator while Asynchronously downloading Image.
I have done it with single image.
But, How I will achieve this in tableView?.Because i'm using NSURLSessionDownloadDelegate and getting progress in URLSession delegate method.
But,How to perform this in tableView where cell's will be reused,different cell's have different images,all of them will have different progress values...?
Here is my code.
class ViewController: UIViewController,NSURLConnectionDataDelegate,NSURLSessionDownloadDelegate {
var session:NSURLSession?
var downloadTask:NSURLSessionDownloadTask?
var downloadURLString = "imageURL"
override func viewDidLoad() {
super.viewDidLoad()
var configuration:NSURLSessionConfiguration = NSURLSessionConfiguration.backgroundSessionConfigurationWithIdentifier("com.app.POCWhatsappLoadingIndicator")
configuration.allowsCellularAccess = true
session = NSURLSession(configuration: configuration, delegate: self, delegateQueue: nil)
var downloadURL:NSURL = NSURL(string: downloadURLString)!
var request:NSURLRequest = NSURLRequest(URL: downloadURL)
downloadTask = self.session?.downloadTaskWithRequest(request)
downloadTask?.resume()
}
Delegate Methods
func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didFinishDownloadingToURL location: NSURL){
var fileManager:NSFileManager = NSFileManager.defaultManager()
var URLs:NSArray = fileManager.URLsForDirectory(NSSearchPathDirectory.DocumentDirectory, inDomains: NSSearchPathDomainMask.UserDomainMask)
var documentsDirectory:NSURL = URLs.objectAtIndex(0) as NSURL
var fromURL:NSURL = downloadTask.originalRequest.URL
var destinationURL:NSURL = documentsDirectory.URLByAppendingPathComponent(fromURL.lastPathComponent!)
var error:NSError?
fileManager.removeItemAtURL(destinationURL, error: nil)
var success:Bool = fileManager.copyItemAtURL(location, toURL: destinationURL, error: &error)
if(success){
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), {
var image:UIImage = UIImage(contentsOfFile: destinationURL.path!)!
UIGraphicsBeginImageContext(CGSizeMake(1, 1))
var context:CGContextRef = UIGraphicsGetCurrentContext()
CGContextDrawImage(context, CGRectMake(0, 0, 1, 1), image.CGImage)
UIGraphicsEndImageContext()
dispatch_sync(dispatch_get_main_queue(), {
self.IBDownloadedImage.image = image
})
})
}else{
println("File Copy failed: \(error)")
}
}
func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64){
var progress:Float = ((Float(totalBytesWritten)/Float(totalBytesExpectedToWrite)))
dispatch_async(dispatch_get_main_queue(), {
self.updateProgress(progress)
})
}
Custom Methods
func updateProgress(progress:Float){
if(progress < 1.0){
self.IBProgressView.setProgress(progress, animated: true)
}
}
That works fine for Single image. But how to do for UITableView.
Thanks.
Upvotes: 0
Views: 1094
Reputation: 2408
Better use SDWebImage library with ActivityIndicator. You can get activity indicator and cache of image also.
link : sdwebimage github
Upvotes: 1
Reputation: 2515
You need to have your progress indicator object in UITableViewCell.
Check following answer. Custom UITableViewCell with Progress Bar download update
Upvotes: 0