YYWebImage icon indicating copy to clipboard operation
YYWebImage copied to clipboard

Use yy_setImage with transform block not working.

Open the-freshlord opened this issue 7 years ago • 8 comments

Hi I am using the method yy_setImageWithURL that has a transform block. Now in the block, I resize the downloaded image and change the color of it. Now for some reason, that block never gets called. I am using Swift 3. Here is the code that I am using if needed:

 mainInterestCell.mainInterestImageView.yy_setImage(with: interest.iconUrl, placeholder: Interest.placeholderImage(), options: [.progressiveBlur, .setImageWithFadeAnimation], progress: nil, transform: { (image: UIImage, imageUrl: URL) -> UIImage? in
    let resizedImage = image.yy_imageByResize(to: CGSize(width: InterestConstants.defaultMainInterestImageWidth, height: InterestConstants.defaultMainInterestImageHeight), contentMode: .scaleAspectFit)
    let color = doesInterestIdExistInCurrentEnthusiast ? colorFromHexValue(StyleConstants.ColorValueLTFLimeGreen) : colorFromHexValue(StyleConstants.ColorValueLTFImageGrey)
    let resizedColorImage = resizedImage?.yy_image(byTintColor: color)
    return resizedColorImage
}, completion: nil)

the-freshlord avatar Oct 07 '16 10:10 the-freshlord

Is there a chance that the image file was fetched earlier without a transform block? If so, any future calls will pull the image from the cache despite the existence of the transform block.

chenium avatar Oct 07 '16 15:10 chenium

So if an image is pulled from the cache, the transform block doesn't get called?

the-freshlord avatar Oct 07 '16 16:10 the-freshlord

Yes, if you follow the code in YYWebImageOperation, you will find that it only calls the transform the first time the image has finished downloading from the web.

Now a way to work around this is to use the option 'YYWebImageOptionAvoidSetImage' and to apply your transform in the completion block instead. You lose the benefit of caching the transformed image but at least it won't have to re-download the image from the web.

A better solution is to implement a 'TransformKey' so that cache key becomes the url + key and that future calls with the same url and key will pull from the cache. I believe there's a pending pull request that fulfills that.

chenium avatar Oct 07 '16 16:10 chenium

So I basically did a check in the completion block to see if the image was pulled from the cache and was able to transform the image.

the-freshlord avatar Oct 07 '16 16:10 the-freshlord

I have the same problem.

ShenYj avatar Nov 29 '16 14:11 ShenYj

I have the same problem.

GTMYang avatar Mar 08 '17 09:03 GTMYang

两个不同的transform,只有前面一个有效,后面的还是使用前面那个transform

- (void)setData:(Trip *)trip {
    [self.imgTrip setNetImageWithName:trip.tripPicName transform:^UIImage * _Nullable(UIImage * _Nonnull image, NSURL * _Nonnull url) {
        return [image yy_imageByRoundCornerRadius:2];
    }];
    self.lbRatingCnt.text = trip.tripRatingCnt;
    [self.imgHead setNetImageWithName:trip.picHead transform:^UIImage * _Nullable(UIImage * _Nonnull image, NSURL * _Nonnull url) {
        return [image yy_imageByRoundCornerRadius: 60]; // 这个不起作用
    }];
    self.lbTripTitle.text = trip.tripTitle;
    self.lbTripPrice.attributedText = trip.tripPriceAttributedString;
    self.lbLabels.text = trip.tripTags;
}

GTMYang avatar Mar 08 '17 09:03 GTMYang

I have the same problem. but i use manager to instead of

YYWebImageManager *manager = [[YYWebImageManager alloc] initWithCache:nil queue:[YYWebImageManager sharedManager].queue];
manager.sharedTransformBlock = ^(UIImage *image, NSURL *url) {
        if (!image) { return image; }
        return [image yy_imageByRoundCornerRadius:100]; // a large value
};
[self.avatar yy_setImageWithURL:URL placeholder:[UIImage imageNamed:@"xxxxxx"] options:kNilOptions manager:manager progress:nil transform:nil completion:nil];

chenjunpu avatar Mar 30 '17 05:03 chenjunpu