您好我需要一些关于此代码的帮助。我按照此链接中的示例进行操作 http://www.raywenderlich.com/85080/beginning-alamofire-tutorial
我在swift 1.x中的代码是,它完美地工作了
extension Alamofire.Request {
class func imageResponseSerializer() -> Serializer {
return { request, response, data in
if data == nil {
return (nil, nil)
}
let image = UIImage(data: data!, scale: UIScreen.mainScreen().scale)
return (image, nil)
}
}
func responseImage(completionHandler: (NSURLRequest, NSHTTPURLResponse?, UIImage?, NSError?) -> Void) -> Self {
return response(serializer: Request.imageResponseSerializer(), completionHandler: { (request, response, image, error) in
completionHandler(request, response, image as? UIImage, error)
})
}
}
但后来我迁移到Swift 2.0,我在互联网上找到了另一个代码,因为第一个代码不再适用。
extension Alamofire.Request {
class func imageResponseSerializer() -> GenericResponseSerializer<UIImage> {
return GenericResponseSerializer { request, response, data in
if data == nil {
return (nil, nil)
}
let image = UIImage(data: data!, scale: UIScreen.mainScreen().scale)
return (image, nil)
}
}
public func responseImage(completionHandler: (NSURLRequest, NSHTTPURLResponse?, AnyObject?, NSError?) -> Void) -> Self {
return response(serializer: Request.responseDataSerializer(), completionHandler: completionHandler)
}
}
struct GenericResponseSerializer<T> : ResponseSerializer {
typealias SerializedObject = T
var serializeResponse: (NSURLRequest?, NSHTTPURLResponse?, NSData?) -> (T?, NSError?)
init(serializeResponse: (NSURLRequest?, NSHTTPURLResponse?, NSData?) -> (T?, NSError?))
}
现在他们都没有在Swift 2.0中工作,而对于最后一个代码,我得到了这些错误:
我称之为在集合视图上显示图像的方式是:
cell.request = Alamofire.request(.GET, imageURL!).responseImage() {
(request, _, image, error) in
if error == nil && image != nil {
if request.URL == cell.request?.request!.URL {
cell.firstphoto.image = image
}
}
}
我已将Alamofire升级到最新版本。 (昨天)
谢谢!
答案 0 :(得分:0)
Alamofire 2.0经过重新设计,请点击此处查看:https://github.com/Alamofire/Alamofire/blob/master/Documentation/Alamofire%202.0%20Migration%20Guide.md
您的代码看起来像这样:
cell.request = Alamofire.request(.GET, imageURL!)
.responseData { request, _, result in
if let error = result.error as? NSError {
//handle error
return
}else{
//Check if image is correct
//result.value
}
}
似乎.responseImage
不再存在。