从UIImage矩形中获取alpha通道

时间:2013-02-21 17:11:52

标签: iphone ios uiimage alpha cgcontext

给定CGRect(rect)和UIImage(image),如何获取矩形中图像像素的alpha值(仅)?

以下代码始终返回0 alpha,无论图像或矩形。

CGContextRef context = CGBitmapContextCreate(NULL,
                                             rect.size.width,
                                             rect.size.height,
                                             8,
                                             rect.size.width,
                                             NULL,
                                             kCGImageAlphaOnly);

UIGraphicsPushContext(context);
[image drawInRect:rect];
char *pixels = CGBitmapContextGetData(context);
NSUInteger pixelCount = rect.size.width * rect.size.height;
for (int i = 0; i < pixelCount; i++)
{
    char pixel = pixels[i];
    CGFloat alpha = pixel / 255.0;
    NSLog(@"%f", alpha);
}
UIGraphicsPopContext();
CGContextRelease(context);

5 个答案:

答案 0 :(得分:0)

pixelData[index + 3]将是Alpha的值。 pixelData[0-2]分别是RGB

CFDataRef theData;

    theData = CGDataProviderCopyData(CGImageGetDataProvider(sourceImage));

    CFMutableDataRef mutableData = CFDataCreateMutableCopy(0, 0, theData);

    UInt8 *pixelData = (UInt8 *) CFDataGetBytePtr(mutableData);

    int dataLength = CFDataGetLength(mutableData);

    for (int index = 0; index < dataLength; index += 4) {
       //code here for just alpha
       pixelData[index + 3]
    }

答案 1 :(得分:0)

过去,我在使CGBitmapContext正常工作时遇到了问题。如果kCGImageAlphaOnly出现问题,我不会感到惊讶。你可以用一些更主流的&#39;进行一些健全的检查。像kCGImageAlphaPremultipliedFirst这样的像素格式。

您也可以尝试使用CGImageDrawImage绘制图像:

CGContextRef context = CGBitmapContextCreate(NULL,
                                         rect.size.width,
                                         rect.size.height,
                                         8,
                                         rect.size.width,
                                         NULL,
                                         kCGImageAlphaOnly);

CGContextDrawImage( context, rect, image.CGImage);

//etc...

CGContextRelease(context);

答案 2 :(得分:0)

根据我对RGBA值的体验,它们都必须是unsigned char。我猜这可能会导致你的问题,因为签名的char会停在127并且如果这个图像的不透明度为1,则永远不会达到完整的255值 - 并且可能更糟糕的是没有正确对齐(读取)。

答案 3 :(得分:0)

我认为这应该可以帮助你...... https://github.com/OgreSwamp/ObjFloodFill

答案 4 :(得分:0)

在Swift 4中,仅获取Alpha通道的代码如下所示:

import UIKit

extension UIImage {
    func alphaData() -> [UInt8]? {
        let size = self.size
        var alphaData = [UInt8](repeating: 0, count: Int(size.width) * Int(size.height))
        let colorSpace = CGColorSpaceCreateDeviceGray()
        let context = CGContext(
            data: &alphaData,
            width: Int(size.width),
            height: Int(size.height),
            bitsPerComponent: 8,
            bytesPerRow: Int(size.width),
            space: colorSpace,
            bitmapInfo: CGImageAlphaInfo.alphaOnly.rawValue
        );

        guard let cgImage = self.cgImage else { return nil }
        context?.draw(cgImage, in: CGRect(x: 0, y: 0, width: size.width, height: size.height))

        return alphaData
    }
}