【问题标题】:Edit image before cache with AFNetworking使用 AFNetworking 在缓存前编辑图像
【发布时间】:2012-12-30 00:06:21
【问题描述】:

我正在使用 AFNetworking 加载一堆图像,我想在 AFNetworking 缓存它们之前对这些图像进行缩放和应用圆角。

我开始在每次加载图像时对图像进行缩放并应用圆角,但是当从缓存加载图像时也会调用完成块,因此当用户滚动填充的集合视图时这会使用太多资源有图片。

[self.imageView setImageWithURLRequest:[NSURLRequest requestWithURL:url
                                                        cachePolicy:NSURLRequestReturnCacheDataElseLoad
                                                    timeoutInterval:10.0f]
placeholderImage:kVideoCollectionViewCellVideoImagePlaceholder
success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
    /**
     * The image is edited here and this block is called
     * when the image is loaded from web and from the cache.
     */
    [self.imageView setImage:image];
}
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
    DDLogError(@"%@", error);
}];

AFNetworking 似乎为我的使用提供了很好的缓存,尤其是当我启用磁盘缓存时,因此我想使用它,但我不知道是否有办法在图像被缓存之前对其进行编辑。

有谁知道这是否可行,如果可以,如何实现?

【问题讨论】:

    标签: objective-c ios afnetworking


    【解决方案1】:

    在发布这个问题后,我突然想到,我可能过于关注使用 UIImageView+AFNetworking 类别的另一个方向。使用 AFImageRequestOperation 直接解决了问题。

    __weak NZVideoCollectionViewCell *weakSelf = self;
    NSURLRequest *request = [NSURLRequest requestWithURL:url
                                             cachePolicy:NSURLRequestReturnCacheDataElseLoad
                                         timeoutInterval:10.0f];
    AFImageRequestOperation *operation = [AFImageRequestOperation imageRequestOperationWithRequest:request imageProcessingBlock:^UIImage*(UIImage *image) {
        /**
         *  Edit image.
         */
        return editedImage;
    }
    success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
        [weakSelf.imageView setImage:image];
    }
    failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
        DDLogError(@"%@", error);
    }];
    [operation start];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-14
      • 2015-10-08
      • 2016-07-16
      相关资源
      最近更新 更多