2014-10-31 151 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 ....你的用户界面保持完好,你得到你的形象...