rdelmar
rdelmar

Reputation: 104082

Snapshot of View Doesn't Render

I'm trying to get a snapshot of another controller's view to use in an animation. I'm using the following code, but I see nothing but the background color of that view, and none of its subviews (two buttons, a text view, and a label):

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    FirstPageController *fpController = [self.storyboard instantiateViewControllerWithIdentifier:@"FirstPage"];
    UIGraphicsBeginImageContext(fpController.view.bounds.size);
    NSLog(@"%@",fpController.view.subviews);
    [fpController.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    CGSize destinationSize = CGSizeMake(90, 122);

    UIGraphicsBeginImageContext(destinationSize);
    [viewImage drawInRect:CGRectMake(0,0,destinationSize.width,destinationSize.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    self.imageView2 = [[UIImageView alloc] initWithImage:newImage];
    self.imageView2.center = self.view.center;
    [self.view addSubview:self.imageView2];
     NSLog(@"%@",NSStringFromCGSize(self.imageView2.image.size));
}

I've logged all the relevant things, the fpController, the imageView, the image, and all are logging correctly.

After Edit: If I switch to the current controller's view, it works fine, so I'm thinking that this has to do with getting a snapshot of a view that's not on screen. Can that be done?

Upvotes: 1

Views: 654

Answers (3)

rdelmar
rdelmar

Reputation: 104082

I found one way to make this work, though it seems like a hack. I add the view I want to render as a subview of my view, do the rendering, and then remove the view. It is never seen on screen, so visually, it's fine. I just wonder whether there's a better way.

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    self.fpController = [self.storyboard instantiateViewControllerWithIdentifier:@"FirstPage"];

    [self.view insertSubview:self.fpController.view belowSubview:self.view];

    UIGraphicsBeginImageContext(self.fpController.view.bounds.size);
    [self.fpController.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    [self.fpController.view removeFromSuperview];

    self.imageView2 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 90, 122)];
    self.imageView2.image = viewImage;
    [self.pageView insertSubview:self.imageView2 belowSubview:self.imageView];
}

Upvotes: 0

Sudha Tiwari
Sudha Tiwari

Reputation: 2451

Hope this will help you:

 CGSize textcontent =CGSizeMake(width, height);
UIGraphicsBeginImageContext(textcontent);
if ([UIScreen instancesRespondToSelector:@selector(scale)] && [[UIScreen mainScreen] scale] == 2.0f)
{
    UIGraphicsBeginImageContextWithOptions(textcontent, YES, 1.0f);
}
else
{
    UIGraphicsBeginImageContext(textcontent);
}
    [image.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return viewImage;

Upvotes: 3

MadhuP
MadhuP

Reputation: 2019

UIGraphicsBeginImageContextWithOptions(fpController.view.bounds.size, NO, 0.0);


UIGraphicsBeginImageContextWithOptions(destinationSize, NO, 0.0);

Use this two lines instead of UIGraphicsBeginImageContext(fpController.view.bounds.size);

 UIGraphicsBeginImageContext(destinationSize); respectively 

hope it help's you

Upvotes: 1

Related Questions