2014-10-31 170 views
0

即使視圖的.alpha屬性設置爲非1值,我也需要獲取視圖快照的完全不透明版本。獲取未應用視圖快照的視圖快照

我通過快速快照獲取iOS中7我的快照:

UIGraphicsBeginImageContextWithOptions(size, NO, 0.0f); 
[self drawViewHierarchyInRect:CGRectMake(0, 0, size.width, size.height) afterScreenUpdates:NO]; 
UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

我期待在這個轉換爲位圖數據,並通過1修改所有的α值的 - view.alpha。我認爲這會工作,但我需要獲得非預乘位圖數據。

CGContextRef context = NULL; 
CGColorSpaceRef colorSpace; 
uint32_t *bitmapData; 

size_t bitsPerPixel = 32; 
size_t bitsPerComponent = 8; 
size_t bytesPerPixel = bitsPerPixel/bitsPerComponent; 

size_t width = CGImageGetWidth(image); 
size_t height = CGImageGetHeight(image); 

size_t bytesPerRow = width * bytesPerPixel; 
size_t bufferLength = bytesPerRow * height; 

colorSpace = CGColorSpaceCreateDeviceRGB(); 

if(!colorSpace) { 
    NSLog(@"Error allocating color space RGB\n"); 
    return NULL; 
} 

// Allocate memory for image data 
bitmapData = (uint32_t *)malloc(bufferLength); 

if(!bitmapData) { 
    NSLog(@"Error allocating memory for bitmap\n"); 
    CGColorSpaceRelease(colorSpace); 
    return NULL; 
} 

//Create bitmap context 
context = CGBitmapContextCreate(bitmapData, 
           width, 
           height, 
           bitsPerComponent, 
           bytesPerRow, 
           colorSpace, 
           (kCGBitmapAlphaInfoMask & kCGImageAlphaLast)); //RGBA 

除非有更簡單的方法去了解這一點,我爲什麼不能得到kCGImageAlphaLast工作? kCGImageAlphaPremultipliedLast似乎工作,但這不會工作。

<Error>: CGBitmapContextCreate: unsupported parameter combination: 8 integer bits/component; 32 bits/pixel; 3-component color space; kCGImageAlphaLast; 8192 bytes/row. 

回答

1

爲什麼不把這個視圖放在containerView中?改變容器的阿爾法,並留下這個'圖像製作視圖'1.0的alpha ....你的用戶界面保持完好,你得到你的形象...