好不容易从国外的网站上 找了段代码是截取opengl屏幕的,但是都只是代码片段, 类的实现我根本不知道是什么样子,也是小菜不懂oc开发的原因, 只是因为需要一个可以截图的插件,捣腾了好几天了.
希望有人帮忙看下
capscreen.h
#import <Foundation/Foundation.h>
@interface UCapture : NSObject
- (UIImage*) currentScreenImage;
- (void)captureToPhotoAlbum;
@end
@interface UIImage (Sudami)
@end
Tweak.xm
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import <IOSurface/IOSurface.h>
#import <pthread.h>
#import <capscreen.h>
#import <OpenGLES/ES2/gl.h>
#import <Opengles/ES2/glext.h>
@implementation UCapture
-(UIImage *) currentScreenImage {
NSInteger myDataLength = 320 * 480 * 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 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 < 480; y++)
{
for(int x = 0; x < 320 * 4; x++)
{
buffer2[(479 - y) * 320 * 4 + x] = buffer[y * 4 * 320 + 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 * 320;
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
// make the cgimage
CGImageRef imageRef = CGImageCreate(320, 480, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);
NSLog(@" imageRef == %@!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!",imageRef);
// then make the uiimage from that
UIImage *myImage = [UIImage imageWithCGImage:imageRef];
return myImage;
}
-(void)captureToPhotoAlbum {
NSLog(@" UIImage *image = [self currentScreenImage];");
UIImage *image = [self currentScreenImage];
NSLog(@" image == %@!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!",image);
UIImageWriteToSavedPhotosAlbum(image, self, nil, nil);
}
@end
void *SocketThread(void *lp)
{
sleep(10);
NSLog(@"CapThread Start !");
// UIView *View = [ [UIView alloc] init] ;
int i = 0 ;
UCapture * aUcapture = [[UCapture alloc] init ];
while(1)
{
[aUcapture captureToPhotoAlbum];
NSLog(@"CapThread index = %d!",i);
sleep(10);
i++;
}
return 0 ;
}
%ctor
{
NSLog(@"Start CapScren dylb;!!!!!!!!!!!!!");
pthread_t ntid;
if ( pthread_create(&ntid, NULL, SocketThread, NULL) == 0 )
{
NSLog(@"SockThread success!");
}
}
现在的情况是截图可以,但是全部都是黑色的图片,希望各位大神帮忙看下 我这个要怎么改。