回答我自己的问题:我通过在我的操作类中覆盖connection:willCacheResponse:
来实现它。
@implementation MYImageRequestOperation
-(CGSize)size
{
return CGSizeZero; // overwritten in subclasses
}
-(NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse
{
UIImage *image = [UIImage imageWithData:cachedResponse.data];
GPUImageLanczosResamplingFilter *resizingFilter = [[GPUImageLanczosResamplingFilter alloc] init];
CGFloat scale = [UIScreen mainScreen].scale;
CGSize size = [self size];
[resizingFilter forceProcessingAtSizeRespectingAspectRatio: CGSizeMake(size.width * scale, size.height * scale)];
GPUImagePicture *gpuImage = [[GPUImagePicture alloc] initWithImage:image];
[gpuImage addTarget:resizingFilter];
[gpuImage processImage];
UIImage *smallImage = [resizingFilter imageFromCurrentlyProcessedOutput];
NSData *data;
if([[cachedResponse.response.MIMEType lowercaseString] isEqualToString:@"image/png"])
{
data = UIImagePNGRepresentation(smallImage);
} else {
data = UIImageJPEGRepresentation(smallImage, .9);
}
NSCachedURLResponse *response = [[NSCachedURLResponse alloc] initWithResponse:cachedResponse.response data:data];
return response;
}
@end