Dynamic Sized DTLazyImageView #307

Closed
jachenry opened this Issue Feb 19, 2013 · 6 comments

Projects

None yet

3 participants

@jachenry
Contributor

We're in the progress of updating our fork with the latest from Cocoenetics and hit an issue dealing with remote images that do not have an explicit width and height set in it's style property. Ever since pulling the latest from master our images are squished.

Up to 1.1.0 we were successfully handling the image in DTAttributedTextContentViewDelegate by waiting for the image to load then resizing it to the lesser of the available space or the original size. This was all handled inside the DTLazyImageViewDelegate method below.

- (void)lazyImageView:(DTLazyImageView *)lazyImageView didChangeImageSize:(CGSize)size {
    NSURL *url = lazyImageView.url;
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"contentURL == %@", url];

    // update all attachments that matchin this URL (possibly multiple images with same size)
    for (DTTextAttachment *oneAttachment in [self.attributedTextContentView.layoutFrame textAttachmentsWithPredicate:pred]) {
        oneAttachment.originalSize = size;
        lazyImageView.bounds = CGRectMake(0, 0, oneAttachment.displaySize.width, oneAttachment.displaySize.height);
    }
    [self.attributedTextContentView relayoutText];
}

The frame of the lazyImage has the correct width and height but it still displays a vertically squashed image. Is there anything special I need to do after updating the image size?

@odrobnik
Collaborator

please look at the updated Demo, you also need to reset the layouter now because otherwise the framesetter and cached frames interfere.

@odrobnik
Collaborator

It now looks like this:

    // need to reset the layouter because otherwise we get the old framesetter or cached layout frames
    _textView.attributedTextContentView.layouter=nil;

    // here we're layouting the entire string, might be more efficient to only relayout the paragraphs that contain these attachments
    [_textView.attributedTextContentView relayoutText];
}
@jachenry
Contributor

Works great. Thanks again Oliver.

@jachenry jachenry closed this Feb 19, 2013
@zen4ever
Contributor

Wow. This should be in the README somewhere. Just spent two hours debugging this issue in my app, and resetting the layouter was exactly what I needed.

@odrobnik
Collaborator

How about you add it to the Programming Guide via PR?

@zen4ever
Contributor

Just sent a PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment