========================================================== =============================== Original blog, reprinted please declare the source of Electronic coffee (original id blue rock) ========================================================== ================================
Share a piece of screen code, which can be a common button, image, and so on.
It is complicated to capture the image of the camera, because the camera is rendered to the view through OpenGL and cannot be captured through the following code.
It's like when we remotely log on to another Windows computer, we can see the desktop of the other party, but we cannot see the video of the other party.
For how to intercept OpenGL view, I will record it in another blog:
IOS screen --- OpenGL screenshot
I have tested it in my project and can use
// Normal screen capture
First introduce the header:
#include <QuartzCore/QuartzCore.h>
/**@view the view you want to take screen shot*/-(UIImage*)getNormalImage:(UIView*)view{ UIGraphicsBeginImageContext(CGSizeMake(320,480)); CGContextRef context = UIGraphicsGetCurrentContext(); [view.layer renderInContext:context]; UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return image;}
// Store uiiamge in the album
//store the image to the iphone photo albumUIImage *image =nil;// init your imageUIImageWriteToSavedPhotosAlbum(image, self, nil, nil);
// Store the uiimage to a local disk, including the PNG and JPEG formats
//store the image to the app Document-(void)saveToDisk:(UIImage*) image{ NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; // If you go to the folder below, you will find those picturesNSLog(@"%@",docDir); NSLog(@"saving png");NSString *pngFilePath = [NSString stringWithFormat:@"%@/test%f.png",docDir,[NSDate timeIntervalSinceReferenceDate]];NSData *data1 = [NSData dataWithData:UIImagePNGRepresentation(image)];[data1 writeToFile:pngFilePath atomically:YES]; NSLog(@"saving jpeg");NSString *jpegFilePath = [NSString stringWithFormat:@"%@/test%f.jpeg",docDir,[NSDate timeIntervalSinceReferenceDate]];NSData *data2 = [NSData dataWithData:UIImageJPEGRepresentation(image, 1.0f)];//1.0f = 100% quality[data2 writeToFile:jpegFilePath atomically:YES]; NSLog(@"saving image done");}
Ref:
Download an image and save it as PNG or JPEG in iPhone SDK export objectgraph blog
IPhone-why do I get 'no-renderincontext: Method Found 'Warning? -Stack Overflow
IPhone-programmatically take a screenshot combining OpenGL and uikit elements-Stack Overflow
IPhone-saving OpenGL ES content to the photo album | b-101
OpenGL ES-why is glreadpixels () Failing in this code in IOS 6.0? -Stack Overflow