2014-05-02 71 views
1

我遷移到AFNetworking 2 什麼是更換AFNetworking 2遷移imageProcessingBlock:

[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]; 
    }]; 

這是雨雲---> NINetworkImageView.m

請建議。

回答

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 

UPDATE:

如果你想要自定義圖像處理邏輯,你應該建立一個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]; 
+0

是的,我明白,但在哪裏放置imageProcessingBlock: – SanjayPathak

+0

@ user3427687,我已經更新了答案。我沒有測試過,但我希望它沒有嚴重的錯誤。 – Avt

+0

@ user3427687如果有用,請不要忘記接受/提出答案。 – Avt

0

感謝上帝,它解決了

我檢查了接收器NT雨雲這是使用AFNetworking 2.2

步驟:

1:複製NIImageResponseSerializer類h和.m文件從最新的項目。

2:從最新項目複製了以下4個功能。

  • (無效)setPathToNetworkImage:(的NSString *)pathToNetworkImage forDisplaySize:(CGSize)顯示尺寸contentMode:(UIViewContentMode)contentMode cropRect:(的CGRect)cropRect
  • (的NSString *)cacheKeyForCacheIdentifier:(的NSString *)cacheIdentifier IMAGESIZE :(CGSize)IMAGESIZE cropRect:(的CGRect)cropRect contentMode:(UIViewContentMode)contentMode scaleOptions:(NINetworkImageViewScaleOptions)scaleOptions
  • (的NSDate *)EXPIRATIONDATE
  • (無效)_didFinishLoadingWithImage:(的UIImage *)圖像 cacheIdentifier:(的NSString *)cacheIdentifier 顯示尺寸:(CGSize)顯示尺寸 cropRect:(的CGRect)cropRect contentMode:(UIViewContentMode)contentMode scaleOptions:(NINetworkImageViewScaleOptions)scaleOptions EXPIRATIONDATE: (NSDate *)expirationDate

那就是它。 作爲參考,我正在遷移到AFNetworking 2.2,它早先添加到我的項目中,我正在使用水平照片滾動功能的Nimbus(舊版本)。