0

I have set up a camera and I managed to add the Cocos2d Layer above the camera, Now I want to take a screenshot of the total view i.e the camera and with the image on the layer above the camera.

Since my CCLayer is made transparent the camera can be seen through.

Any ideas/links/details on how to do this.. ?

Thanks in advance for your time :)

EDIT: THIS IS MY CODE FOR THE SCREENSHOT

UIGraphicsBeginImageContext([CCDirector sharedDirector].openGLView.superview.frame.size);
    [[CCDirector sharedDirector].openGLView.superview.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndPDFContext();

    CGRect rect;
    rect = CGRectMake(0, 0, 480, 320);
    CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage],[CCDirector sharedDirector].openGLView.superview.frame);
    UIImage * img =[UIImage imageWithCGImage:imageRef];
    CGImageRelease(imageRef);
    UIImageWriteToSavedPhotosAlbum(img, nil, nil, nil);
Shailesh
  • 3,072
  • 3
  • 24
  • 33

2 Answers2

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

//It will give you the uiimage

Prabhjot Singh Gogana
  • 1,408
  • 1
  • 14
  • 39
  • Since I am using Cocos2d ..! Self.view.layer wont work ..! How to use this code in Cocos2d.. ?I tried some work around, But all I get is Black/White screen as a screeshot. – Shailesh Sep 13 '12 at 07:44
  • I tried that too, But still I get just black screen as a screenShot. Also please have a look at my EDIT in question, I have put some code that I am using for the SCreenShot. – Shailesh Sep 13 '12 at 08:08
  • I used them, I used AVFoundation, QuartzCore and CoreGraphics as well..! – Shailesh Sep 13 '12 at 08:09
  • //use this UIGraphicsEndImageContext(); u use UIGraphicsEndPDFContext(); – Prabhjot Singh Gogana Sep 13 '12 at 08:14
  • Still a black screen :( Anything wrong the way I am using this code ? – Shailesh Sep 13 '12 at 08:17
  • I do get it it NSlog, it shows that some memory is allocated, But the screenShot gives black screen. – Shailesh Sep 13 '12 at 08:21
  • check with uiimageview UIImageView *invertedImage = [[UIImageView alloc] initWithImage:viewImage]; [invertedImage setFrame:CGRectMake(0, 0, 320, 480)]; – Prabhjot Singh Gogana Sep 13 '12 at 08:21
  • Thanks a lot..!! But its for the UIKit, It cant be used in Cocos2d. Iam looking for Cocos2d ..! Using it may affect the performance. – Shailesh Sep 13 '12 at 08:44
0

To all those who are looking for the same thing ..! I have got it working using AVFoundation framework. This is one nice tutorial on how to achieve this :)

Shailesh
  • 3,072
  • 3
  • 24
  • 33