1

一个需要解决的功能:后台桌面录像我使用NSTimer循环调用IOSurface进行截图,如下代码:</p>

NSTimer * Timer = [NSTimer scheduledTimerWithTimeInterval:0.1f target:self selector:@selector(ScreenShot) userInfo:nil repeats:YES];

-(void)ScreenShot{io_service_t framebufferService;
framebufferService = IOServiceGetMatchingService(kIOMasterPortDefault, IOServiceMatching("AppleH1CLCD"));
if (!framebufferService) {
    framebufferService = IOServiceGetMatchingService(kIOMasterPortDefault, IOServiceMatching("AppleM2CLCD"));
}
if (!framebufferService) {
    framebufferService = IOServiceGetMatchingService(kIOMasterPortDefault, IOServiceMatching("AppleCLCD"));
}
IOMobileFramebufferConnection connect;
kern_return_t result;
IOSurfaceRef ScreenSurface;
result = IOMobileFramebufferOpen(framebufferService, mach_task_self(), 0, &connect);
result = IOMobileFramebufferGetLayerDefaultSurface(connect, 0, &ScreenSurface);uint32_t aseed;
uint32_t Width;
uint32_t Height;
int Pitch;
int size;
int bPE;
CFMutableDictionaryRef dict;
IOSurfaceLock(ScreenSurface, kIOSurfaceLockReadOnly, &aseed);
Width = IOSurfaceGetWidth(ScreenSurface);
Height = IOSurfaceGetHeight(ScreenSurface);
Pitch = Width * 4;
size = Width * Height * 4;
bPE = 4;
char pixelFormat[4] = {'A','R','G','B'};
dict = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
CFDictionarySetValue(dict, kIOSurfaceIsGlobal, kCFBooleanTrue);
CFDictionarySetValue(dict, kIOSurfaceBytesPerRow, CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &Pitch));
CFDictionarySetValue(dict, kIOSurfaceBytesPerElement, CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &bPE));
CFDictionarySetValue(dict, kIOSurfaceWidth, CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &Width));
CFDictionarySetValue(dict, kIOSurfaceHeight, CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &Height));
CFDictionarySetValue(dict, kIOSurfacePixelFormat, CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, pixelFormat));
CFDictionarySetValue(dict, kIOSurfaceAllocSize, CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &size));

CGDataProviderRef provider;
IOSurfaceRef destSurf;
void* outAcc;
CGImageRef cgImage;
destSurf = IOSurfaceCreate(dict);
IOSurfaceAcceleratorCreate(NULL, 0, &outAcc);
IOSurfaceAcceleratorTransferSurface(outAcc, ScreenSurface, destSurf, dict, NULL);
IOSurfaceUnlock(ScreenSurface, kIOSurfaceLockReadOnly, &aseed);
CFRelease(outAcc);
provider = CGDataProviderCreateWithData(NULL, IOSurfaceGetBaseAddress(destSurf), (Width * Height * 4), NULL);
cgImage = CGImageCreate(Width, Height, 8, 8*4, IOSurfaceGetBytesPerRow(destSurf), CGColorSpaceCreateDeviceRGB(), kCGImageAlphaNoneSkipFirst | kCGBitmapByteOrder32Little, provider, NULL, YES, kCGRenderingIntentDefault);}

可以截图成功。</p>

现在的问题是:

  1. 但大概运行 1 分钟左右,提示“Received memory warning”。</li>
  2. 滑动屏幕,屏幕方法会延迟

或者有什么方法可以后台桌面录像?</p>

4

0 回答 0