用于imageProcessingBlock的AFNetworking 2迁移:

时间:2014-05-02 11:39:36

标签: ios

我正在迁移到AFNetworking 2 什么是

的替代品
[AFImageRequestOperation imageRequestOperationWithRequest:request imageProcessingBlock:
     ^UIImage *(UIImage *downloadedImage) {
         return [NIImageProcessing imageFromSource:downloadedImage
                                   withContentMode:contentMode
                                          cropRect:cropRect
                                       displaySize:displaySize
                                      scaleOptions:self.scaleOptions
                              interpolationQuality:self.interpolationQuality];

     } success:^(NSURLRequest *successfulRequest, NSHTTPURLResponse *response, UIImage *processedImage) {
         [self _didFinishLoadingWithImage:processedImage
                          cacheIdentifier:pathToNetworkImage
                              displaySize:displaySize
                              contentMode:contentMode
                             scaleOptions:self.scaleOptions
                           expirationDate:nil];

     } failure:^(NSURLRequest *errorRequest, NSHTTPURLResponse *response, NSError *error) {
         [self _didFailToLoadWithError:error];
     }];

这是来自Nimbus ---> NINetworkImageView.m

请建议。

2 个答案:

答案 0 :(得分:2)

在AFNetworking 2.0中,您应该使用AFHTTPRequestOperation的属性

@property (nonatomic, strong) AFHTTPResponseSerializer <AFURLResponseSerialization> * responseSerializer;

设置响应应该如何序列化的方式。在您的情况下,您需要AFImageResponseSerializer

operation.responseSerializer = [AFImageResponseSerializer serializer];

要设置完成块,请使用AFHTTPRequestOperation的方法:

- (void)setCompletionBlockWithSuccess:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
                              failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure;

要创建操作,您还可以使用AFHTTPRequestOperationManager的方法

- (AFHTTPRequestOperation *)GET:(NSString *)URLString
                     parameters:(id)parameters
                        success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
                        failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure

更新:

如果你想拥有自定义图像处理逻辑,你应该创建一个AFImageResponseSerializer子类并实现方法

- (id)responseObjectForResponse:(NSURLResponse *)response
                           data:(NSData *)data
                          error:(NSError *__autoreleasing *)error

例如:

@interface MyImageResponseSerializer : AFImageResponseSerializer 
@end

@implementation MyImageResponseSerializer
- (id)responseObjectForResponse:(NSURLResponse *)response
                           data:(NSData *)data
                          error:(NSError *__autoreleasing *)error
{
     UIImage *img = [super responseObjectForResponse:response
                           data:data
                          error:error];
     // add a watermark to img or do something else
     return img;
}

@end

然后将其设置为操作的responseSerializer属性

operation.responseSerializer = [MyImageResponseSerializer serializer];

答案 1 :(得分:0)

感谢上帝,已经解决了

我检查了最近使用AFNetworking 2.2的Nimbus

步骤:

1:从最新项目中复制NIImageResponseSerializer类.h和.m文件。

2:从最新项目中复制以下4个功能。

  • (void)setPathToNetworkImage:(NSString *)pathToNetworkImage forDisplaySize:(CGSize)displaySize contentMode:(UIViewContentMode)contentMode cropRect:(CGRect)cropRect
  • (NSString *)cacheKeyForCacheIdentifier:(NSString *)cacheIdentifier                            IMAGESIZE:(CGSize)IMAGESIZE                             cropRect:(的CGRect)cropRect                          contentMode:(UIViewContentMode)contentMode                         scaleOptions:(NINetworkImageViewScaleOptions)scaleOptions
  • (NSDate *)expirationDate
  • (void)_didFinishLoadingWithImage:(UIImage *)图片                cacheIdentifier:(NSString *)cacheIdentifier                    显示尺寸:(CGSize)显示尺寸                       cropRect:(的CGRect)cropRect                    contentMode:(UIViewContentMode)contentMode                   scaleOptions:(NINetworkImageViewScaleOptions)scaleOptions                 expirationDate:(NSDate *)expirationDate

多数民众赞成。 作为参考,我正在迁移到之前添加到我的项目中的AFNetworking 2.2,我使用Nimbus(旧版本)进行水平照片滚动功能。