【发布时间】:2014-05-01 19:31:11
【问题描述】:
我正在尝试使用 Core Graphics 开发绘图应用程序。我想让背景处于 alpha 状态,但它是黑色的。我尝试使用所有不同类型的 bitmapinfo 类型,但没有成功。 kCGImageAlphaPremultipliedLast 也不起作用。有谁知道如何解决这个问题?
- (BOOL) initContext:(CGSize)size {
int bitmapByteCount;
int bitmapBytesPerRow;
// Declare the number of bytes per row. Each pixel in the bitmap in this
// example is represented by 4 bytes; 8 bits each of red, green, blue, and
// alpha.
bitmapBytesPerRow = (size.width * 4);
bitmapByteCount = (bitmapBytesPerRow * size.height);
// Allocate memory for image data. This is the destination in memory
// where any drawing to the bitmap context will be rendered.
cacheBitmap = malloc( bitmapByteCount );
if (cacheBitmap == NULL){
return NO;
}
CGBitmapInfo bitmapInfo = kCGImageAlphaNoneSkipFirst;
cacheContext = CGBitmapContextCreate (cacheBitmap, size.width, size.height, 8, bitmapBytesPerRow, CGColorSpaceCreateDeviceRGB(), bitmapInfo);
CGContextSetRGBFillColor(cacheContext, 0, 0, 0, 0);
CGContextFillRect(cacheContext, (CGRect){CGPointZero, size});
return YES;
}
【问题讨论】:
-
这里的代码没有指定 alpha 通道(因此 AlphaNone)我想你要找的可能是
kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrderDefault -
另外,正如所写,我相信你正在泄漏
CGColorSpaceCreateDeviceRGB()分配的内存 -
你能解释更多关于泄漏内存的内容吗?
-
还是不行。
-
你如何确定它不起作用?
标签: ios objective-c uiview core-graphics