kye kye - 7 months ago 92
Swift Question

How to use AlamofireImage ProgressHandler

Im having a hard to figuring out how exactly I can define the

ProgressHandler
parameter. The
typealias
is defined as
public typealias ProgressHandler = (bytesSent: Int64, totalBytesSent: Int64, totalExpectedBytes: Int64) -> Void
More information can be found here https://github.com/Alamofire/AlamofireImage/pull/91

let URLRequest = NSURLRequest(URL: NSURL(string: "https://httpbin.org/image/jpeg")!)
ImageDownloader().downloadImages(URLRequests: [URLRequest], filter: nil,
progress: (init progress here), progressQueue: dispatch_get_main_queue(), completion: {
_ in
})


NOT A DUPLICATE! Answer/question referenced is what lead to this feature being implemented http://stackoverflow.com/a/33503205/5222077

Answer

Define it as you would any closure in Swift. You can give the captured parameters any names you want, or none using _, but you need to have 3. For instance:

ImageDownloader().downloadImage(URLRequest: "http://httpbin.org/image/png", progress: { (bytesRead, totalBytesRead, totalExpectedBytesToRead) in
    print("Read:\(bytesRead), Total Read: \(totalBytesRead), Expected: \(totalExpectedBytesToRead)")
})