51

显然没有AFImageRequestOperation,但AFImageResponseSerializer坦率地说,我不明白,或者我只是在 AFNetworking 网站上看了太久......用以前的 AFNetworking 下载图像就像一种魅力。我不想回到旧的 AFnetworking,因为我几乎所有的东西都是通过新版本完成的……有人吗?

4

3 回答 3

126

所以你想要2.0这样的东西。

AFHTTPRequestOperation *requestOperation = [[AFHTTPRequestOperation alloc] initWithRequest:urlRequest];
requestOperation.responseSerializer = [AFImageResponseSerializer serializer];
[requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"Response: %@", responseObject);
    _imageView.image = responseObject;

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Image error: %@", error);
}];
[requestOperation start];

正如亚当所提到的,如果您只是想将其放入 imageView 中,您也可以执行以下操作

[myImageView setImageWithURL:[NSURL URLWithString:@"http://sitewithimage.com/images/myimage.png"]];
于 2013-10-21T18:41:08.830 回答
4

老版本没有responseSerializer,你也可以

AFHTTPRequestOperation *requestOperation = [[AFHTTPRequestOperation alloc] initWithRequest:urlRequest];
//requestOperation.responseSerializer = [AFImageResponseSerializer serializer];
[requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"Response: %@", responseObject);
    _imageView.image = [UIImage imageWithData:responseObject];

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Image error: %@", error);
}];
[requestOperation start];
于 2014-04-24T21:17:40.863 回答
0

对于AFNetworking在 Swift 中使用的人,上面的解决方案可以写成如下

    let requestOperation : AFHTTPRequestOperation = AFHTTPRequestOperation(request: urlRequest)
    requestOperation.responseSerializer = AFImageResponseSerializer()

    requestOperation.setCompletionBlockWithSuccess({ (requestOperation, responseObject) in
       print(responseObject)
        _imageView.image = responseObject as? UIImage

    }) { (requestOperation, error) in
       print(error)
    }
    requestOperation.start()
于 2016-06-29T11:31:12.807 回答