Last active
August 11, 2016 07:53
-
-
Save winzig/8bf72a6a6c4b70257dec to your computer and use it in GitHub Desktop.
Ray Wenderlich's UIImage serializer for Alamofire, updated for Alamofire 3.0 and Swift 2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension Alamofire.Request { | |
/** Response serializer for images from: http://www.raywenderlich.com/85080/beginning-alamofire-tutorial */ | |
public static func imageResponseSerializer() -> ResponseSerializer<UIImage, NSError> { | |
return ResponseSerializer { request, response, data, error in | |
guard let validData = data else { | |
let failureReason = "Data could not be serialized. Input data was nil." | |
let error = Error.errorWithCode(.DataSerializationFailed, failureReason: failureReason) | |
return .Failure(error) | |
} | |
if let image = UIImage(data: validData, scale: UIScreen.mainScreen().scale) { | |
return .Success(image) | |
} | |
else { | |
return .Failure(Error.errorWithCode(.DataSerializationFailed, failureReason: "Unable to create image.")) | |
} | |
} | |
} | |
/** Convenience method for returning images from: http://www.raywenderlich.com/85080/beginning-alamofire-tutorial */ | |
func responseImage(completionHandler: Response<UIImage, NSError> -> Void) -> Self { | |
return response(responseSerializer: Request.imageResponseSerializer(), completionHandler: completionHandler) | |
} | |
} |
Many thanks! I needed to update some code to work with Alamofire 3.0 and I was struggling. This is exactly what I need!
This may help either.
struct Five100px {
enum Router: URLRequestConvertible {
static let baseURLString = "https://api.500px.com/v1"
static let consumerKey = "PASTE_YOUR_CONSUMER_KEY_HERE"
case PopularPhotos(Int)
case PhotoInfo(Int, ImageSize)
case Comments(Int, Int)
var URLRequest: NSMutableURLRequest {
let result: (path: String, parameters: [String: AnyObject]) = {
switch self {
case .PopularPhotos (let page):
let params = ["consumer_key": Router.consumerKey, "page": "\(page)", "feature": "popular", "rpp": "50", "include_store": "store_download", "include_states": "votes"]
return ("/photos", params)
case .PhotoInfo(let photoID, let imageSize):
let params = ["consumer_key": Router.consumerKey, "image_size": "\(imageSize.rawValue)"]
return ("/photos/\(photoID)", params)
case .Comments(let photoID, let commentsPage):
let params = ["consumer_key": Router.consumerKey, "comments": "1", "comments_page": "\(commentsPage)"]
return ("/photos/\(photoID)/comments", params)
}
}()
let URL = NSURL(string: Router.baseURLString)
let URLRequest = NSURLRequest(URL: URL!.URLByAppendingPathComponent(result.path))
let encoding = Alamofire.ParameterEncoding.URL
return encoding.encode(URLRequest, parameters: result.parameters).0
}
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example of how you might use this with Alamofire 3.0: