how to get the alpha channel alone in IOS?

╄→гoц情女王★ 提交于 2019-12-07 17:28:10

问题


I have the image and I need to produce the image of alpha coordinates only... I did the following but it produces the red colored image only what I did wrong in the code...

Thanks in advance .... 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];
}


回答1:


Check if this works. This function returns you autoreleased alpha image.

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


来源:https://stackoverflow.com/questions/11686489/how-to-get-the-alpha-channel-alone-in-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!