This question is an extension to:
Link-1: Creating an image out of the ios surface and saving it
Link-2: Taking Screenshots from iOS app - emulating Display recorder (query on the internals)
(Refer Link-1) I have the code for taking screenshots in the background working. But as stated, it requires a sleep of 2 sec to work uninterruptedly, otherwise the OS suspends the app. I figured out the reason could be that I am not explicitly releasing the IOSurface I create.
Reason - Using the link http://pastie.org/pastes/3734430 given by Victor Ronin, the capture works fine even without the sleep.
I tried releasing the destSurf (destination surface that I have created) using CFRelease after I write the image each time, but that doesn't work.
Any help on when, how and whether or not to release a created IOSurface, will be really useful. Thanks.
Update
So, this is what exactly happens. (Referring Link-1)
IOSurfaceRef destSurf = IOSurfaceCreate(dict);
IOSurfaceAcceleratorRef outAcc;
IOSurfaceAcceleratorCreate(NULL, 0, &outAcc);
CFDictionaryRef ed = (__bridge CFDictionaryRef)[NSDictionary dictionaryWithObjectsAndKeys: nil];
IOSurfaceAcceleratorTransferSurface(outAcc, screenSurface, destSurf, ed, NULL);
IOSurfaceUnlock(screenSurface, kIOSurfaceLockReadOnly, &aseed);
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, IOSurfaceGetBaseAddress(destSurf), (width*height*4), NULL);
CGImageRef cgImage=CGImageCreate(width, height, 8, 8*4, IOSurfaceGetBytesPerRow(destSurf), CGColorSpaceCreateDeviceRGB(), kCGImageAlphaNoneSkipFirst | kCGBitmapByteOrder32Little, provider, NULL, YES, kCGRenderingIntentDefault);
UIImage *image = [UIImage imageWithCGImage: cgImage];
CGImageRelease(cgImage);
UIImageWriteToSavedPhotosAlbum(image, self, nil, nil);
CFRelease(destSurf);
- The call to CFRelease takes place and app crashes. There are no images saved. However, if you put in a sleep of 1-2 sec prior to the release, it works fine.
- If you skip the call to UIImageWriteToSavePhotosAlbum(), everything works fine and capture goes without interruption.
I am still not clear on what exactly is the problem and how to solve it. Any help is highly appreciated.