Taking screenshot of NGLView (NineVehGL engine) - ios

I am using NinevehGL 3D engine http://nineveh.gl/
I want to get screenshot of OpenGL object. Problem is with below code
-(UIImage *) screenshot
{
// Capture screen here... and cut the appropriate size for saving and uploading
UIGraphicsBeginImageContext([self theNGLView].bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// crop the area you want
CGRect rect;
rect = CGRectMake(0, 10, 300, 300); // whatever you want
CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], rect);
UIImage *img = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
return img;
}
Only background comes in screenshot but not actual NGLView or OpenGL object which is drawn on top of it.
I am using iOS SDK 6.0

tried using
[[self the NGLView].layer renderInContext:UIGraphicsGetCurrentContext()];
?

Please go through this link, you need to download the latest NinevehGL SDK for iPhone5/iOS 6 which is provided in their twitter link.

Related

Default iPhone screenshot programmatically

I tried two different ways to create a screenshot, but unfortunately they don't work as I need, I have an RMMapView that is blank on the screenshot. When I create snapshot manually on my device it works perfectly, and the map view is on the screen. So I would like to achieve the same result programmatically. Is it possible somehow as I tried? Or what is the right way to do that? (To reproduce that type of screenshot)
- (UIImage *) takeScreenshot {
//1. version
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, [UIScreen mainScreen].scale);
[self.view drawViewHierarchyInRect:self.view.bounds afterScreenUpdates:YES];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
//2. version
UIGraphicsBeginImageContext(self.view.bounds.size);
CGContextRef context=UIGraphicsGetCurrentContext();
[self.view.layer renderInContext:context];
UIImage *image=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
Actually You can use a method called takeSnapshot in a RMMapView.
UIImage *image = self.mapView.takeSnapshot
[Update]
Can you try this method instead
CGSize size = self.view.bounds.size;
CGRect cropRect = self.mapView.bounds
UIGraphicsBeginImageContext(size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * mapImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGImageRef imageRef = CGImageCreateWithImageInRect(mapImage.CGImage, cropRect);
UIImage * cropImage = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
UIImageWriteToSavedPhotosAlbum(cropImage, nil, nil, nil);
UIGraphicsEndImageContext();
Good luck

Take snap shot - only part of UIView, not the whole frame

I can take snapshots of the whole frame of the UIView with no problem.
Taking part of it cause to a squeezed image.
This is my UIView category method:
- (UIImage *)snapshot:(CGRect)frame
{
UIGraphicsBeginImageContextWithOptions(frame.size, YES, [[UIScreen mainScreen] scale]);
[self drawViewHierarchyInRect:frame afterScreenUpdates:YES];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
And this is the call to this method:
UIImage *img = [self.view snapshot:bottomRectToSnap];
When bottomRectToSnap is a partial frame of the view.
Note:The UIView contains UITableView if that matter.
The code you are applying is fine.
Need to add some logic, whatever part you need to take snap, It should be available on screen and hide rest of part while snapping.
Once snap shot done, remove hide code. So it will not effect to view interface too.
Use the following code.
- (void)captureView:(UIView*)view withFrame:(CGRect)frame{
UIGraphicsBeginImageContext(self.view.bounds.size);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGRect rect = frame;
CGImageRef imageRef = CGImageCreateWithImageInRect([viewImage CGImage], rect);
UIImage *img = [UIImage imageWithCGImage:imageRef];
UIImageWriteToSavedPhotosAlbum(img, nil, nil, nil);
CGImageRelease(imageRef);
}
You can add this code to take the snapshot of particular part..
CGRect cropRect=CGRectMake(0, 0, 100, 100);
CGImageRef imageRef = CGImageCreateWithImageInRect([_galleryimageview.image CGImage], cropRect);
UIImage *cropedImage=[UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
This code crop the part of particular image ...Hope this is helpfull for you..

Taking screenshot in Objective-C without losing quality [duplicate]

This question already has answers here:
UIImage screenshot loses its quality
(5 answers)
Closed 8 years ago.
What I'm trying to achieve is taking a screenshot in my app which is working fine, but the quality is very poor, and the size of the screenshot is also pretty small. I also try to crop the bottom of the image as I did for the top, but I cannot see where I can add dimensions for the bottom. Is there a better way to do this? I'm using the code below:
// Define the dimensions of the screenshot you want to take (the entire screen in this case)
UIGraphicsBeginImageContext(self.view.bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *sourceImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//now we will position the image, X/Y away from top left corner to get the portion we want
UIGraphicsBeginImageContext(self.view.frame.size);
[sourceImage drawAtPoint:CGPointMake(0, -10)];
UIImage *croppedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(croppedImage,nil, nil, nil);
Thank you for your answer.
replace
UIGraphicsBeginImageContext(self.view.bounds.size);
with
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, self.view.opaque, 0.0);
This is the code I use to save the contents of a view to 2 png files (low and high resolution) onto the desktop.
// High resolution
UIGraphicsBeginImageContextWithOptions(drawView.bounds.size, NO, 0.0);
[[drawView layer] renderInContext:UIGraphicsGetCurrentContext()];
UIImage *ViewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *pngData = UIImagePNGRepresentation(ViewImage);
[pngData writeToFile:#"/Users/_username_/Desktop/Image#2x.png" atomically:NO];
// Low resolution
UIGraphicsBeginImageContext(drawView.bounds.size);
[[drawView layer] renderInContext:UIGraphicsGetCurrentContext()];
ViewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
pngData = UIImagePNGRepresentation(ViewImage);
[pngData writeToFile:#"/Users/_username_/Desktop/Image.png" atomically:NO];

How to zoom in and scale uiimage

Im new to cropping images in Xcode, and in my app I take a picture, and put that picture into a uiimage that is in a circler form. What I want to do is zoom in or move the image while the image is in the circler view then save it to the final uiimage. What I have so far is
CALayer *imageLayer= imageView.layer;
imageView.layer.cornerRadius =imageView.frame.size.height/2;
[imageLayer setBorderWidth:.5];
[imageLayer setMasksToBounds:YES];
[self.view addSubview:imageView];
Please help as soon as possible! Im so confused.
Following code creates subimage from rect rectangle of your_image with zooming parameter scale
CGImageRef imageRef = CGImageCreateWithImageInRect([your_image CGImage], rect);
UIImage *subImage = [UIImage imageWithCGImage:imageRef scale:scale orientation:your_image.imageOrientation];
CGImageRelease(imageRef);

iOS capture UIView with UIImageView inside

I'm trying to get a UIView screen capture on iOS.
In my view I have two UIImageView with animations.
The code that I using it to capture the view is:
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, 1.0);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
// Read the UIImage object
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(image, self,
#selector(image:didFinishSavingWithError:contextInfo:), nil);
The result is an image with the background and only one UIImageView. Both ImageViews are in moviming all the time. If I take several pictures, then the UIImageView is in the same position each time.
Try this one:
-(CGImageRef)imageCapture
{
UIGraphicsBeginImageContext(self.view.bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGRect rect= CGRectMake(0,0 ,width, height);
CGImageRef imageRef = CGImageCreateWithImageInRect([viewImage CGImage], rect);
return imageRef;
}
use the below line whenever you want to capture the screen
UIImage *captureImg=[[UIImage alloc] initWithCGImage:[self imageCapture]];

Resources