kean/DFImageManager

sometimes empty image

Closed this issue · 8 comments

hello

i am in the process of porting to DFImageManager from SDWebImage :) - one of the main reason, the GIF support.

right now i have a few collectionviews (inside uitabbarcontroller) each with a bunch of cells (mtl.image is DFImageView).

doing something like

[mtl.image prepareForReuse];
[mtl.image setAllowsAnimations:NO];
[mtl.image setImageWithResource:[NSURL URLWithString:obj.imageUrl]];

loading the same datasource, into two tabs (collection views) - sometimes does not load images in one collectionview, and in the other it does (mainly on GIFS), i have no clue where to start debugging, any hints?

regards
helmut

kean commented

Thanks, I'll take a look into it. My first guess is that it is somehow related to the way FLAnimatedImageView renders images. There is a lot of stuff going on in FLAnimatedImage class and you are trying to display the same FLAnimatedImage instance in two separate image views.

kean commented

Hi @hjanuschka, did you figure out what was the issue?

i actually removed dfimagemanager, and stayed with SDWebimage (Atleast for now)
had following problems beside the problem with flanimated image

*) image loading didn't seem to be canceled, wich resulted in a really slow loading behavior on fast scrolling

the FLAnimated image problem, seems to be, that we cannot change the flanimated image i came up with the following solution:

using: https://github.com/jagbolanos/FLAnimatedImage_AFNetworking (for async loading of flanimatedimages)

inside my cellforitematindexpath

[[mtl.image subviews]    makeObjectsPerformSelector:@selector(removeFromSuperview)];
if([obj.imageUrl_Orig hyp_containsString:@"gif"]) {
        [mtl.image setImage:nil];


        FLAnimatedImageView * faiv = [[FLAnimatedImageView alloc] initWithFrame:CGRectMake(0,0, mtl.image.frame.size.width, mtl.image.frame.size.height)];
        [mtl.image setAutoresizesSubviews:YES];
        faiv.contentMode=mtl.image.contentMode;

        faiv.autoresizingMask=UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
        faiv.clipsToBounds=YES;
        __weak __typeof(faiv)weakfaiv = faiv;
        NSURLRequest * ur=[[NSURLRequest alloc] initWithURL:[NSURL URLWithString:obj.imageUrl]];

        [faiv setAnimatedImageWithURLRequest:ur
                            placeholderImage:nil
                                     success:^(NSURLRequest *request, NSHTTPURLResponse *response, FLAnimatedImage *animatedImage){
                                         weakfaiv.animatedImage = animatedImage;

                                         //do more things
                                     } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error){
                                         //handle failure
                                     }];


        [mtl.image addSubview: faiv];

    } else {

        [mtl.image sd_setImageWithURL:[NSURL URLWithString:  obj.imageUrl] placeholderImage:nil options:SDWebImageCacheMemoryOnly completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            if (image && cacheType == SDImageCacheTypeNone)
            {


            }

        }];
    }

i would absolutly love to change to dfimagemanager (right now keeping changes in a branch) - once the loading performance, and animated image stuff is solved.

kean commented

Hi @hjanuschka

*) image loading didn't seem to be canceled, wich resulted in a really slow loading behavior on fast scrolling

Do you have a way to reproduce this, preferably on version 0.7.0? Please show how you configure DFImageManager (or which subspecs you installed if you don't change sharedManager) and the way you start and cancel image requests.

using: https://github.com/jagbolanos/FLAnimatedImage_AFNetworking (for async loading of flanimatedimages)

Thanks, I'll take a look at this.

kean commented

I've done some extensive testing of GIFs and it actually works really well https://youtu.be/YctDm3L4uXw. Tested with both NSURLSession and AFNetworking subspecs from develop branch.

I've also made some minor changes in _DFURLFetcherTaskQueue that would allow for instant NSURLSessionTask cancellation. Previous releases were resuming and cancelling tasks with a micro-delay to prevent NSURLSession trashing during extremely fast scrolling. This change only affects DFImageManager/NSURLSession subspec.

New release (0.7.1) would be available shortly, your feedback would be much appreciated.

kean commented

I actually did find and fix an issue with cancellation of fetch operations (see #71). It's a rare issue, but it might happen quiet frequently depending on the way you start and cancel image requests.

kean commented

Regarding cancellation. It's also a matter of how you implement UITableView/UICollectionView delegate. Here is a way to cancel requests immediately when cell goes off screen.

- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
    DFImageView *imageView = (id)[cell viewWithTag:15];
    if (!imageView) {
        imageView = [[DFImageView alloc] initWithFrame:cell.bounds];
        imageView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        imageView.tag = 15;
        [cell addSubview:imageView];
    }

    NSURL *URL = _photos[indexPath.row];
    [imageView setImageWithResource:URL targetSize:[self _imageTargetSize] contentMode:DFImageContentModeAspectFill options:nil];
}

- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
    DFImageView *imageView = (id)[cell viewWithTag:15];
    [imageView prepareForReuse];
}

Bad thing is here

DFImageView *imageView = (id)[cell viewWithTag:15];

You should check type of "imageView"

if (!imageView || ![imageView isKindOfClass:[DFImageView class]])

instead of

if (!imageView)

because imageView sometimes becomes UICollectionViewCell-like class