2014-05-01 53 views
0

我正在嘗試開發一個使用Core Graphics的繪圖應用程序。我想讓背景處於alpha狀態,而不是黑色。我嘗試使用所有不同類型的bitmapinfo類型,但沒有成功。 kCGImageAlphaPremultipliedLast也不起作用。任何人都知道如何解決此問題?CGbitmapcontext和alpha問題

- (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; 
} 
+0

你在這裏的代碼沒有指定alpha通道(因此AlphaNone)我認爲你要找的可能是'kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrderDefault' –

+0

另外,正如所寫的,我相信你正在泄漏由'CGColorSpaceCreateDeviceRGB()'分配的內存' –

+0

你能解釋更多的泄漏內存嗎? – user3534757

回答

1

我正在使用此代碼來完成您要求的功能。我將顏色設置爲紅色,而不是0%,所以你可以看到alpha通道在那裏。

@implementation ViewController 
{ 
    CGContextRef     cacheContext; 
    void*       cacheBitmap; 
    __weak IBOutlet UIImageView* _imageView; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
} 

-(void)viewDidAppear:(BOOL)animated 
{ 
    [super viewDidAppear:animated]; 

    [self setupContext:self.view.bounds.size]; 

    CGImageRef  cgImage = CGBitmapContextCreateImage(cacheContext); 
    _imageView.image = [UIImage imageWithCGImage:cgImage]; 
    CGImageRelease(cgImage); 
} 


// Name changed to avoid using the magic word "init" 
- (BOOL) setupContext:(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 = kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrderDefault; 

    // Create and define the color space 
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 

    cacheContext = CGBitmapContextCreate (cacheBitmap, size.width, size.height, 8, bitmapBytesPerRow, colorSpace, bitmapInfo); 
    CGContextSetRGBFillColor(cacheContext, 1., 0, 0, 0.5); 
    CGContextFillRect(cacheContext, (CGRect){CGPointZero, size}); 

    // Release the color space so memory doesn't leak 
    CGColorSpaceRelease(colorSpace); 

    return YES; 
} 

@end 
+0

謝謝!還發現,我沒有把我的觀點設置爲[UIColor ClearColor] – user3534757