3

我有图像,我只需要生成 alpha 坐标的图像......我做了以下但它只产生红色图像我在代码中做错了......

在此先感谢.... myCode:

-(UIImage*)alphaAlone:(UIImage*)sourceImage
{
     CGImageRef img=sourceImage.CGImage;
    CFDataRef m_DataRef; 
    m_DataRef = CGDataProviderCopyData(CGImageGetDataProvider(img)); 
    UInt8 *dataMasked=(UInt8 *)CFDataGetBytePtr(m_DataRef);
    double length=CFDataGetLength(m_DataRef);

    NSLog(@"length::%f",length);


for(int index=0;index<length;index+=4)
    {

            dataMasked[index]  =dataMasked[index];
            dataMasked[index+1]  =0;
            dataMasked[index+2]  =0;
            dataMasked[index+3]  =0;
     }

 NSUInteger width =CGImageGetWidth(img);
    size_t height=CGImageGetHeight(img);
    size_t bitsPerComponent=CGImageGetBitsPerComponent(img);
    size_t bitsPerPixel=CGImageGetBitsPerPixel(img);
    size_t bytesPerRow=CGImageGetBytesPerRow(img);

    NSLog(@"the w:%u H:%lu",width,height);

    CGColorSpaceRef colorspace=CGImageGetColorSpace(img);
    CGBitmapInfo bitmapInfo=CGImageGetBitmapInfo(img);

    CFDataRef newData=CFDataCreate(NULL,dataMasked,length);
    CGDataProviderRef provider=CGDataProviderCreateWithCFData(newData);
    CGImageRef newImg=CGImageCreate(width,height,bitsPerComponent,bitsPerPixel,bytesPerRow,colorspace,bitmapInfo,provider,NULL,true,kCGRenderingIntentDefault);

    return [UIImage imageWithCGImage:newImg];
}

结果

4

1 回答 1

1

检查这是否有效。此函数返回您自动发布的 alpha 图像。

- (UIImage*)seperatAlphaFromImage:(UIImage*)pngImage writeToFile:(NSString*)path compressionQuality:(float)value0To1
{    
CGRect imageRect = CGRectMake(0, 0, pngImage.size.width, pngImage.size.height);

//Pixel Buffer
uint32_t* piPixels = (uint32_t*)malloc(imageRect.size.width * imageRect.size.height * sizeof(uint32_t));
if (piPixels == NULL)
{
    return nil;
}
memset(piPixels, 0, imageRect.size.width * imageRect.size.height * sizeof(uint32_t));

//Drawing image in the buffer
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(piPixels, imageRect.size.width, imageRect.size.height, 8, sizeof(uint32_t) * imageRect.size.width, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedLast);

CGContextDrawImage(context, imageRect, pngImage.CGImage);

//Copying the alpha values to the red values of the image and setting the alpha to 1
for (uint32_t y = 0; y < imageRect.size.height; y++) 
{
    for (uint32_t x = 0; x < imageRect.size.width; x++)
    {
        uint8_t* rgbaValues = (uint8_t*)&piPixels[y * (uint32_t)imageRect.size.width + x];

        //alpha = 0, red = 1, green = 2, blue = 3.

        rgbaValues[0] = rgbaValues[0];
        rgbaValues[1] = rgbaValues[0];
        rgbaValues[2] = rgbaValues[0];
        rgbaValues[3] = rgbaValues[0];
    }
}

//Creating image whose red values will preserve the alpha values
CGImageRef newCGImage = CGBitmapContextCreateImage(context);
UIImage* newImage = [[[UIImage alloc] initWithCGImage:newCGImage]autorelease];
CGImageRelease(newCGImage);

return newImage;    
}
于 2012-07-27T12:02:51.880 回答