Code:
- (char *)getFramebuffer
{
CGImageRef imageRef = CGDisplayCreateImage(displayID);
if(imageRef == NULL)
return NULL;
CGContextRef context = [SIZE="3"][COLOR="Red"]CreateARGBBitmapContext[/COLOR][/SIZE](imageRef );
if(context == NULL)
return NULL;
size_t w = CGImageGetWidth(imageRef );
size_t h = CGImageGetHeight(imageRef);
CGRect rect = {{0,0},{w,h}};
rfbLog("111");
CGContextDrawImage(context, rect,imageRef );
rfbLog("222");
char *returnValue = (char *)CGBitmapContextGetData(context);
return returnValue;
}
///////////////////////////////////////////////
CGContexRef CreateARGBBitmapContext(CGImageRef inImage)
{
int bitmapBytesPerRow;
int bitmapByteCount;
CGColorSpaceRef colorSpace;
char *bitmapData;
CGContextRef context = NULL;
size_t pixelsWidth = CGImageGetWidth(inImage);
size_t pixelsHeight = CGImageGetHeight(inImage);
bitmapBytesPerRow = pixelsWidth * 4;
bitmapByteCount = bitmapBytesPerRow * pixelsHeight ;
colorSpace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);
if(colorSpace == NULL )
return NULL;
bitmapData = (char *)malloc(sizeof(char ) * bitmapByteCount );
if(bitmapData== NULL )
{
CGColorSpaceRelease(colorSpace);
return NULL;
}
context = CGBitmapContextCreate(bitmapData,
pixelsWidth ,
pixelsHeight,
8,
bitmapBytesPerRow ,
colorSpace ,
kCGImageAlphaPremultipliedLast);
if(context == NULL)
free(bitmapData);
CGColorSpaceRelease(colorSpace);
return context;
}
Code always error "CGImageDataLock : cannot allocate memory" after printing "111".
I spent so much time on it but still don't find the reason。
THKS!!!
Last edited by a moderator: