Alamofire 3.0.0-beta.3图像响应序列化

时间:2015-10-01 09:08:27

标签: serialization swift2 ios9 xcode7 alamofire

有人可以帮助我,我正在尝试实现Alamofire图像响应序列化方法:imageResponseSerializer,

这是我的代码:

extension Alamofire.Request {
    class func imageResponseSerializer() -> ResponseSerializer<UIImage, NSError> {
        return ResponseSerializer<UIImage, NSError> { 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 Result<UIImage, NSError>.Success(image)
            }
            else {
                return .Failure(Error.errorWithCode(.JSONSerializationFailed, failureReason: "Unable to create image."))
            }
        }
    }

    func responseImage(completionHandler: (NSURLRequest?, NSHTTPURLResponse?, Result<UIImage, NSError>) -> Void) -> Self {
        return response(responseSerializer: Request.imageResponseSerializer(), completionHandler: { request, response, result in
            completionHandler(request, response, result)
        })
    }
}

错误:无法调用非函数类型的值'NSHTTPURLResponse?'

我正在使用:Xcode 7.0.1,Swift 2和Alamofire 3.0.0-beta.3

谢谢,

1 个答案:

答案 0 :(得分:0)

你应该看看AlamofireImage。 1)已经实现了所有这些。 2)它有许多其他很棒的功能,你很可能会发现它们很方便。