 
 import UIKit
 
+/// A subclass of UIImageView which displays a JPEG progressively while it is downloaded
 public class CCBufferedImageView : UIImageView, NSURLConnectionDataDelegate {
     private weak var connection: NSURLConnection?
     private let defaultContentLength = 5 * 1024 * 1024

        connection?.cancel()
    }

    public override init(frame: CGRect) {
        super.init(frame: frame)
    }

    public init(URL: NSURL) {
        super.init()

        load(URL)
    }

    required public init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    public func load(URL: NSURL) {
        connection = NSURLConnection(request: NSURLRequest(URL: URL), delegate: self)
    }

    // MARK: NSURLConnectionDataDelegate

    public func connection(connection: NSURLConnection, didFailWithError error: NSError) {
        NSLog("Error: %@", error)
    }

    public func connection(connection: NSURLConnection, didReceiveData data: NSData) {
        self.data?.appendData(data)


        image = decoder.toImage()
    }

    /// see NSURLConnectionDataDelegate
    public func connection(connection: NSURLConnection, didReceiveResponse response: NSURLResponse) {
        var contentLength = Int(response.expectedContentLength)
        if contentLength < 0 {
