2

I have a collection view that scrolls horizontally. I need to create a UIImageView from the currently visible portion of the collection view.

I usually use the following method for this:

+ (UIImageView *) imageCopyOfView:(UIView *)inputView
{
    UIGraphicsBeginImageContext(inputView.bounds.size);
    [inputView.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    UIImageView *retView = [[UIImageView alloc] initWithImage:viewImage];
    return retView;
}

but it does not work with the Collection View after it has been scrolled as it seems to be getting a portion of the view that has been scrolled off the screen

RegularExpression
  • 3,531
  • 2
  • 25
  • 36

1 Answers1

1

Instead of

[inputView.layer renderInContext:UIGraphicsGetCurrentContext()];

you should use

[inputView drawViewHierarchyInRect:inputView.frame afterScreenUpdates:YES];
augustzf
  • 2,385
  • 1
  • 16
  • 22
  • Well, it is different but not quite it. But I'm using the captured image in an animation that may be messing it up somewhere. Continuing to look at it. That may have gotten it but I could just have something else causing a problem. Thanks for the reply. I'll keep working with it. – RegularExpression May 08 '14 at 21:46
  • 1
    After some tinkering I found the problem was a problem with the frame in the method I was calling it from. Your solution was exactly what I need. Thanks. – RegularExpression May 09 '14 at 06:20