IOS螢幕截圖—Opengl截屏

來源:互聯網
上載者:User

============================================================博文原創,轉載請聲明出處電子咖啡(原id藍岩)============================================================

上篇blog介紹了如何進行普通:IOS螢幕---普通

前面已經說過,使用普通方式截屏,不能能夠截取Opengl渲染的View。就像我們遠程登入了另一台windows電腦時候,能看到對方的案頭等,但對方的視頻我們並不能看到。

對於此類view,我們需要將opengl渲染的view,通過glReadPixels將其所有的像素讀取到數組中,然後通過這些像素資料建立UIImage,詳細代碼如下。

在我們opengl view,實現下面方法

//In Your GL View

//convert opengl data to UIImage-(UIImage *) glToUIImage { CGSize viewSize=self.frame.size; NSInteger myDataLength = viewSize.width * viewSize.height * 4; // allocate array and read pixels into it. GLubyte *buffer = (GLubyte *) malloc(myDataLength); glReadPixels(0, 0, viewSize.width, viewSize.height, GL_RGBA, GL_UNSIGNED_BYTE, buffer); // gl renders "upside down" so swap top to bottom into new array. // there's gotta be a better way, but this works. GLubyte *buffer2 = (GLubyte *) malloc(myDataLength); for(int y = 0; y < viewSize.height; y++) { for(int x = 0; x < viewSize.width* 4; x++) { buffer2[(int)((viewSize.height-1 - y) * viewSize.width * 4 + x)] = buffer[(int)(y * 4 * viewSize.width + x)]; } } // make data provider with data. CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, buffer2, myDataLength, NULL); // prep the ingredients int bitsPerComponent = 8; int bitsPerPixel = 32; int bytesPerRow = 4 * viewSize.width; CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB(); CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault; CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault; // make the cgimage CGImageRef imageRef = CGImageCreate(viewSize.width , viewSize.height, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent); // then make the uiimage from that UIImage *myImage = [UIImage imageWithCGImage:imageRef]; return myImage;}

另外,我在實現這段代碼時候,一下代碼得到的buffer依然為空白。

glReadPixels(0, 0, viewSize.width, viewSize.height, GL_RGBA, GL_UNSIGNED_BYTE, buffer);

google上解釋說,這是ios6的問題。需要在CAEAGLLayer初始化時候設定一下屬性kEAGLDrawablePropertyRetainedBacking 為YES。

如下

CAEAGLLayer *eaglLayer = (CAEAGLLayer *) self.layer;eaglLayer.drawableProperties = @{    kEAGLDrawablePropertyRetainedBacking: [NSNumber numberWithBool:YES],    kEAGLDrawablePropertyColorFormat: kEAGLColorFormatRGBA8};

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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.