使用 UIProgressView Indicator 在 UITableView 中下载图像
Image download in UITableView with UIProgressView Indicator
我想在异步下载图片时显示进度指示器。
我已经用单张图片完成了。
但是,我将如何在 tableView 中实现这一点?因为我正在使用 NSURLSessionDownloadDelegate 并在 URLSession 委托方法中取得进展。
但是,如何在将重复使用单元格的 tableView 中执行此操作,不同的单元格具有不同的图像,它们都将具有不同的进度值...?
这是我的代码。
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()
}
委托方法
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)
})
}
自定义方法
func updateProgress(progress:Float){
if(progress < 1.0){
self.IBProgressView.setProgress(progress, animated: true)
}
}
这对单张图片来说效果很好。
但是如何为UITableView.
做
谢谢。
您需要在 UITableViewCell 中包含进度指示器对象。
检查以下答案。
Custom UITableViewCell with Progress Bar download update
最好将 SDWebImage 库与 ActivityIndicator 一起使用。您还可以获得 activity 指示器和图像缓存。
link : sdwebimage github
我想在异步下载图片时显示进度指示器。
我已经用单张图片完成了。
但是,我将如何在 tableView 中实现这一点?因为我正在使用 NSURLSessionDownloadDelegate 并在 URLSession 委托方法中取得进展。
但是,如何在将重复使用单元格的 tableView 中执行此操作,不同的单元格具有不同的图像,它们都将具有不同的进度值...?
这是我的代码。
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()
}
委托方法
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)
})
}
自定义方法
func updateProgress(progress:Float){
if(progress < 1.0){
self.IBProgressView.setProgress(progress, animated: true)
}
}
这对单张图片来说效果很好。 但是如何为UITableView.
做谢谢。
您需要在 UITableViewCell 中包含进度指示器对象。
检查以下答案。 Custom UITableViewCell with Progress Bar download update
最好将 SDWebImage 库与 ActivityIndicator 一起使用。您还可以获得 activity 指示器和图像缓存。
link : sdwebimage github