Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Swift 4 Compatibility - No NSURLConnection Refactor #21

Merged
merged 12 commits into from
Jan 30, 2018
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 26 additions & 26 deletions Code/CCBufferedImageView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
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
private var data: NSMutableData?
private let queue = dispatch_queue_create("com.contentful.Concorde", DISPATCH_QUEUE_SERIAL)
open class CCBufferedImageView : UIImageView, NSURLConnectionDataDelegate {
fileprivate weak var connection: NSURLConnection?
fileprivate let defaultContentLength = 5 * 1024 * 1024
fileprivate var data: Data?
fileprivate let queue = DispatchQueue(label: "com.contentful.Concorde", attributes: [])

/// Optional handler which is called after an image has been successfully downloaded
public var loadedHandler: (() -> ())?
open var loadedHandler: (() -> ())?

deinit {
connection?.cancel()
Expand All @@ -26,72 +26,72 @@ public class CCBufferedImageView : UIImageView, NSURLConnectionDataDelegate {
public override init(frame: CGRect) {
super.init(frame: frame)

backgroundColor = UIColor.grayColor()
backgroundColor = UIColor.gray
}

/// Initialize a new image view and start loading a JPEG from the given URL
public init(URL: NSURL) {
public init(URL: Foundation.URL) {
super.init(image: nil)

backgroundColor = UIColor.grayColor()
backgroundColor = UIColor.gray
load(URL)
}

/// Required initializer, not implemented
required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)

backgroundColor = UIColor.grayColor()
backgroundColor = UIColor.gray
}

/// Load a JPEG from the given URL
public func load(URL: NSURL) {
open func load(_ URL: Foundation.URL) {
connection?.cancel()
connection = NSURLConnection(request: NSURLRequest(URL: URL), delegate: self)
connection = NSURLConnection(request: URLRequest(url: URL), delegate: self)
}

// MARK: NSURLConnectionDataDelegate

/// see NSURLConnectionDataDelegate
public func connection(connection: NSURLConnection, didFailWithError error: NSError) {
NSLog("Error: %@", error)
open func connection(_ connection: NSURLConnection, didFailWithError error: Error) {
NSLog("Error: \(error)")
}

/// see NSURLConnectionDataDelegate
public func connection(connection: NSURLConnection, didReceiveData data: NSData) {
self.data?.appendData(data)
open func connection(_ connection: NSURLConnection, didReceive data: Data) {
self.data?.append(data)

dispatch_sync(queue) {
let decoder = CCBufferedImageDecoder(data: self.data)
decoder.decompress()
queue.sync {
let decoder = CCBufferedImageDecoder(data: self.data! as Data)
decoder?.decompress()

guard let decodedImage = decoder.toImage() else {
guard let decodedImage = decoder?.toImage() else {
return
}

UIGraphicsBeginImageContext(CGSizeMake(1,1))
UIGraphicsBeginImageContext(CGSize(width: 1,height: 1))
let context = UIGraphicsGetCurrentContext()
CGContextDrawImage(context, CGRectMake(0, 0, 1, 1), decodedImage.CGImage)
context?.draw(decodedImage.cgImage!, in: CGRect(x: 0, y: 0, width: 1, height: 1))
UIGraphicsEndImageContext()

dispatch_async(dispatch_get_main_queue()) {
DispatchQueue.main.async {
self.image = decodedImage
}
}
}

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

data = NSMutableData(capacity: contentLength)
data = Data(capacity: contentLength)
}

/// see NSURLConnectionDataDelegate
public func connectionDidFinishLoading(connection: NSURLConnection) {
open func connectionDidFinishLoading(_ connection: NSURLConnection) {
data = nil

if let loadedHandler = loadedHandler {
Expand Down
4 changes: 2 additions & 2 deletions Concorde.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "Concorde"
s.version = "0.1.0"
s.version = "0.1.1"
s.summary = "Download and decode progressive JPEGs easily."
s.homepage = "https://github.com/contentful-labs/Concorde/"
s.social_media_url = 'https://twitter.com/contentful'
Expand All @@ -15,7 +15,7 @@ Pod::Spec.new do |s|
:tag => s.version.to_s }
s.requires_arc = true

s.ios.deployment_target = '8.0'
s.ios.deployment_target = '10.0'
s.ios.frameworks = 'UIKit'
s.osx.deployment_target = '10.9'

Expand Down
Loading