0

I'm trying to create a UIView where I add 2 UIImageView inside and create an UIImage from this. Here is the method I use, but I get a blank UIImage.

Here is the code I use :

-(UIImage*)getFinalImage{

    CGRect rect = CadreImage.frame;

    UIView *dynaView = [[UIView alloc] initWithFrame:rect];
    UIImageView *frontCadre = [[UIImageView alloc] initWithImage:TheImage];

    [dynaView addSubview:frontCadre];

    UIGraphicsBeginImageContextWithOptions(dynaView.bounds.size, dynaView.opaque, 0.0);
    [dynaView.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return img;
}

Any help would be appreciated.

Thanks.

Bahaïka
  • 699
  • 1
  • 11
  • 36
  • 1
    NSLog `rect`, `frontCadre`, etc. My guess is a messed up frame along the way. – Michael Boselowitz Jul 17 '12 at 16:45
  • frame seems okay : `2012-07-17 18:49:20.203 Pixamaz[1095:907] w:320.000000 2012-07-17 18:49:20.215 Pixamaz[1095:907] h:367.000000` Where H and W represent height and width. – Bahaïka Jul 17 '12 at 16:50
  • oh did you ever add the dynaview to the screen? I believe you have to add that as a subview of your current view so the graphics can actually pull the pixel data in – Msencenb Jul 17 '12 at 17:10
  • I tried that too `[self.view addSubview:dynaView];` – Bahaïka Jul 17 '12 at 17:15
  • Can you view the image if you just add the subview to the view instead of trying to take a picture? – Msencenb Jul 17 '12 at 21:37

1 Answers1

0

Here is the code I use to grab a layer/screenshot (note this is for the whole screen and not a specific layer + handles retina displays):

if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)])
    UIGraphicsBeginImageContextWithOptions(self.view.window.bounds.size, NO, [UIScreen mainScreen].scale);
else
    UIGraphicsBeginImageContext(self.view.bounds.size);
[self.view.window.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

Can you also set a breakpoint and jump through the program to verify that TheImage is not nil?

Msencenb
  • 5,675
  • 11
  • 52
  • 84