gpt4 book ai didi

iOS CGBitmapContextCreate 复制数据吗?

转载 作者:行者123 更新时间:2023-11-28 18:25:56 25 4
gpt4 key购买 nike

这段代码是我写的:

 bitmapData = calloc(1, bitmapByteCount );
context = CGBitmapContextCreate (bitmapData,
pixelsWide,
pixelsHigh,
8,
bitmapBytesPerRow,
colorSpace,
kCGImageAlphaOnly);

当我这样做时,CGBitmapContext 是否正在复制我的位图数据,所以在这些行之后我应该写

free(bitmapData); 

最佳答案

如果您需要bitmapData,请不要释放它。如果您不需要它,请将 NULL 作为参数传递,Quartz 将自行分配内存(iOS 4.0 及更高版本)。

data: A pointer to the destination in memory where the drawing is to be rendered. The size of this memory block should be at least (bytesPerRow*height) bytes. In iOS 4.0 and later, and Mac OS X v10.6 and later, you can pass NULL if you want Quartz to allocate memory for the bitmap. This frees you from managing your own memory, which reduces memory leak issues.

但是 Quartz 不复制 bitmapData,它在那里进行渲染。释放 context 后,您应该释放该内存。

编辑:在 Apple 示例之一中 projects ,内存被释放,但不是立即释放:

float drawStage3(CGContextRef context, CGRect rect)
{
// ...
cachedData = malloc( (((ScaledToWidth * 32) + 7) / 8) * ScaledToHeight);
// ...
bitmapContext = CGBitmapContextCreate(cachedData /* data */,
// ...
CFRelease(bitmapContext);
// ...
// Clean up
CFRelease(cachedImage);
free(cachedData);
}

关于iOS CGBitmapContextCreate 复制数据吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9444295/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com