Sergey Grishchev
Sergey Grishchev

Reputation: 12051

Can't get a screenshot of UIView to be displayed inside UIImageView

Here's my code to get the screenshot of the view:

if (NULL != UIGraphicsBeginImageContextWithOptions){
    UIGraphicsBeginImageContextWithOptions(pagedScrollView.frame.size, NO, [[UIScreen mainScreen] scale]);
}else{
    UIGraphicsBeginImageContext(pagedScrollView.frame.size);
}

[pagedScrollView.layer renderInContext:UIGraphicsGetCurrentContext()];
screenshotImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

UIImageView *imageView = [[UIImageView alloc] initWithImage:screenshotImage];
imageView.frame = CGRectMake(imageView.frame.origin.x, imageView.frame.origin.y, imageView.frame.size.width, imageView.frame.size.height);
imageView.backgroundColor = [UIColor greenColor];
[self.view addSubview:imageView];

I'm 100% sure I have the right frame and the right view - the size of UIImageView say about it - but the thing is I can't get my UIImage to be displayed on the UIImageView.

The UIImageView is added on the view and I see a green region on it - but no image inside it, what can be wrong?

I'm using iOS 6 and ARC.

Upvotes: 0

Views: 82

Answers (2)

Sergey Grishchev
Sergey Grishchev

Reputation: 12051

It turns out I've been taking a screenshot of a long UIScrollView and I was always taking its first page - it had a lot of pages in it and sometimes the layer had nothing in it. It also turns out that UIImage was never released and the memory can effectively store dozens of them.

Upvotes: 0

Mick MacCallum
Mick MacCallum

Reputation: 130183

Well, I just tested your code running on my view controller's view and the capture and display worked fine. This leads me to believe that pagedScrollView is probably nil, or has a zero frame.

Either way, you'll be creating an image from a blank context and then passing it to your image view resulting in there not being any visible image.

Upvotes: 1

Related Questions