============================================================博文原創,轉載請聲明出處電子咖啡(原id藍岩)============================================================
分享一段螢幕的代碼,能夠普通的button,image,等。
對於截取網路攝影機的映像,比較複雜,因為網路攝影機是通過opengl渲染到view上面的,通過下面的代碼並不能截取。
就像我們遠程登入了另一台windows電腦時候,能看到對方的案頭等,但對方的視頻我們並不能看到。
關於截取opengl view的方法,我會在另一篇部落格中記錄:
IOS螢幕---Opengl截屏
我已經在我的工程中測試過,可以使用
//普通螢幕截取
首先引入頭:
#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;}
//將UIIamge儲存到相簿中
//store the image to the iphone photo albumUIImage *image =nil;// init your imageUIImageWriteToSavedPhotosAlbum(image, self, nil, nil);
//儲存UIImage到本地磁碟,包括png和jpeg格式
//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 ‹ 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 | BIT-101
opengl es - Why is glReadPixels() failing in this code in iOS 6.0? - Stack Overflow