- Sort Posts
- 9 replies
- Last post
Take screenshot
Please can you avoid double posting your question, because it causes confusion and delays.
Secondly in future can you please create a new thread rather than taking over an existing thread.
I will deal with your question here:
https://developer.vuforia.com/forum/ios/vuforia-sdk-how-take-screenshots
N
Take screenshot
Hi,
I got a half black screen on my album, When i take screenShot.
I written following code below :-
@interface VBOverlayViewController : OverlayViewController {
UIButton *takePhoto;
}
@property (nonatomic,assign)UIButton *takePhoto;
//For Take a screen Shot
-(UIImage *)glToUIImage;
-(void)TakePhoto;
@implementation VBOverlayViewController
@synthesize takePhoto;
-(void)loadView
{
takePhoto = [[UIButton alloc]initWithFrame:CGRectMake(0, 14, 70, 70)];
// takePhoto.backgroundColor = [UIColor colorWithPatternImage:[]];
[takePhoto setTitle:@"Take Photo" forState:UIControlStateNormal];
[takePhoto.titleLabel setFont:[UIFont systemFontOfSize:12.0]];
[takePhoto addTarget:self action:@selector(TakePhoto) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:takePhoto];
}
-(UIImage *)glToUIImage
{
NSInteger myDataLength = 320 * 480 * 8;//4
//allocate array and read pixels into it.
GLubyte *buffer = (GLubyte *)malloc(myDataLength);
glReadPixels(0, 0, 320, 480, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
//gl renders "upside down" so swap top bottom into new array.
//there's gotta be a better way, but this works.
GLubyte *buffer2 = (GLubyte *)malloc(myDataLength);
for(int y = 0; y<480; y++)
{
for (int x=0; x<320; x++) {
buffer2[(479 - y) * 320 * 4 + x] = buffer[y * 4 * 320 + x];//4
}
}
//make data provider with data.
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, buffer2, myDataLength, NULL);
//prep the ingredients
int bitsPerComponent = 8;//8
int bitPerPixel = 32;//64
int bytePerRow = 4 * 320;//4
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
//make the cgimage
CGImageRef imageRef = CGImageCreate(320, 480, bitsPerComponent, bitPerPixel, bytePerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);
NSLog(@"imageRef:-%@",imageRef);
//then make the uiImage from that
UIImage *myImage = [UIImage imageWithCGImage:imageRef];
NSLog(@"myImage:-%@",myImage);
return myImage;
}
-(void)TakePhoto
{
UIImage *image = [self glToUIImage];
NSLog(@"imge:-%@",image);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Get It!" message:@"Go to the Album." delegate:nilcancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
UIImageWriteToSavedPhotosAlbum(image, self, nil, nil);
}
@implementation EAGLView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
CAEAGLLayer *eaglLayer = (CAEAGLLayer *)self.layer;
eaglLayer.opaque = TRUE;
// eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:
// [NSNumber numberWithBool:YES], kEAGLDrawablePropertyRetainedBacking,
// kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat,
// nil];
eaglLayer.drawableProperties = @{
kEAGLDrawablePropertyRetainedBacking: [NSNumber numberWithBool:YES],
kEAGLDrawablePropertyColorFormat: kEAGLColorFormatRGBA8,
};
What I do, because I m doing all codes.Please guide me.
Thank you
Khushi
Re: Take screenshot
Re: Take screenshot
Tank You MoSR,
I try to make screenshot whit this method but I can't.
Were exactly I must insert the code in Xcode?:eek:
In my case, i used the code directly in an IBAction, and called it from a button in InterfaceBuilder. It's a bit of a shabby method, since it requires that you overlay a button as UI on top of your EAGLView, but here goes:
-(IBAction)facebookPhoto:(id)sender { UIImage *outputImage = nil; CGFloat scale = [[UIScreen mainScreen] scale]; CGRect s = CGRectMake(0, 0, 320.0f * scale, 480.0f * scale); uint8_t *buffer = (uint8_t *) malloc(s.size.width * s.size.height * 4); glReadPixels(0, 0, s.size.width, s.size.height, GL_RGBA, GL_UNSIGNED_BYTE, buffer); CGDataProviderRef ref = CGDataProviderCreateWithData(NULL, buffer, s.size.width * s.size.height * 4, NULL); CGImageRef iref = CGImageCreate(s.size.width, s.size.height, 8, 32, s.size.width * 4, CGColorSpaceCreateDeviceRGB(), kCGBitmapByteOrderDefault, ref, NULL, true, kCGRenderingIntentDefault); size_t width = CGImageGetWidth(iref); size_t height = CGImageGetHeight(iref); size_t length = width * height * 4; uint32_t *pixels = (uint32_t *)malloc(length); CGContextRef context = CGBitmapContextCreate(pixels, width, height, 8, width * 4, CGImageGetColorSpace(iref), kCGImageAlphaNoneSkipFirst | kCGBitmapByteOrder32Big); CGAffineTransform transform = CGAffineTransformIdentity; transform = CGAffineTransformMakeTranslation(0.0f, height); transform = CGAffineTransformScale(transform, 1.0, -1.0); CGContextConcatCTM(context, transform); CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, width, height), iref); CGImageRef outputRef = CGBitmapContextCreateImage(context); outputImage = [UIImage imageWithCGImage: outputRef]; CGDataProviderRelease(ref); CGImageRelease(iref); CGContextRelease(context); CGImageRelease(outputRef); free(pixels); free(buffer); UIImageWriteToSavedPhotosAlbum(outputImage, nil, nil, nil); }
This code will take a snapshot of what's currently on your EAGLView (including both camera and 3D content), and save it as a photo to your iPhone Photo Album.
Hi Nalin,
I posted my quary many time, but you never give solution once a time. If you can give me solution then give me advice.
Khushi