美文网首页iOS开发
iOS-删除图片透明通道,并将rgb图片转换为bgr图片

iOS-删除图片透明通道,并将rgb图片转换为bgr图片

作者: 拎着猫走的鱼 | 来源:发表于2020-09-16 14:47 被阅读0次

    可以根据自己的需求修改代码,不需要转bgr的可以注释掉转换部分

    • OC代码
    + (NSData *)getBGRWithImage:(UIImage *)image
    {
        int RGBA = 4;
        int RGB  = 3;
        
        CGImageRef imageRef = [image CGImage];
        
        size_t width = CGImageGetWidth(imageRef);
        size_t height = CGImageGetHeight(imageRef);
        
        CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
        unsigned char *rawData = (unsigned char *) malloc(width * height * sizeof(unsigned char) * RGBA);
        NSUInteger bytesPerPixel = RGBA;
        NSUInteger bytesPerRow = bytesPerPixel * width;
        NSUInteger bitsPerComponent = 8;
        CGContextRef context = CGBitmapContextCreate(rawData, width, height, bitsPerComponent, bytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
        
        CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
        
        CGColorSpaceRelease(colorSpace);
        CGContextRelease(context);
        
        unsigned char * tempRawData = (unsigned char *)malloc(width * height * 3 * sizeof(unsigned char));
        
        for (int i = 0; i < width * height; i ++) {
            
            NSUInteger byteIndex = i * RGBA;
            NSUInteger newByteIndex = i * RGB;
            
            // Get RGB
            CGFloat red    = rawData[byteIndex + 0];
            CGFloat green  = rawData[byteIndex + 1];
            CGFloat blue   = rawData[byteIndex + 2];
            //CGFloat alpha  = rawData[byteIndex + 3];// 这里Alpha值是没有用的
            
            // Set RGB To New RawData
            tempRawData[newByteIndex + 0] = blue;   // B
            tempRawData[newByteIndex + 1] = green;  // G
            tempRawData[newByteIndex + 2] = red;    // R
        }
        
        NSData *data = [NSData dataWithBytes:tempRawData length:(width * height * 3 * sizeof(unsigned char))];
        return data;
    }
    
    • Swift5 代码
        func getBGRWithImage(image : UIImage) -> Data? {
            
            let RGBA = 4;
            let RGB  = 3;
            
            let imageRef = image.cgImage!
            
            let width = imageRef.width;
            let height = imageRef.height;
            
            let colorSpace = CGColorSpaceCreateDeviceRGB()
            
            let rawData = malloc(width * height * MemoryLayout<CUnsignedChar>.size * RGBA)!.assumingMemoryBound(to: UInt8.self)
            let bytesPerPixel = RGBA;
            let bytesPerRow = bytesPerPixel * width;
            let bitsPerComponent = 8;
            let bitmapInfo = CGBitmapInfo(
              rawValue: CGBitmapInfo.byteOrder32Big.rawValue | CGImageAlphaInfo.premultipliedLast.rawValue
            )
            let context = CGContext.init(data: rawData, width: width, height: height, bitsPerComponent: bitsPerComponent, bytesPerRow: bytesPerRow, space: colorSpace, bitmapInfo: bitmapInfo.rawValue)
            context?.draw(imageRef, in: CGRect(x: 0, y: 0, width: width, height: height))
            
            let tempRawData = malloc(width * height * 3 * MemoryLayout<CUnsignedChar>.size)!.assumingMemoryBound(to: UInt8.self)
            
            for i in 0..<width * height {
                
                let byteIndex = i * RGBA;
                let newByteIndex = i * RGB;
                
                // Get RGB
                let red    = rawData[byteIndex + 0];
                let green  = rawData[byteIndex + 1];
                let blue   = rawData[byteIndex + 2];
                //CGFloat alpha  = rawData[byteIndex + 3];// 这里Alpha值是没有用的
                
                // Set RGB To New RawData
                tempRawData[newByteIndex + 0] = blue;   // B
                tempRawData[newByteIndex + 1] = green;  // G
                tempRawData[newByteIndex + 2] = red;    // R
            }
            
            let data = Data(bytes: tempRawData, count: (width * height * 3 * MemoryLayout<CUnsignedChar>.size))
            
            return data
        }
    

    相关文章

      网友评论

        本文标题:iOS-删除图片透明通道,并将rgb图片转换为bgr图片

        本文链接:https://www.haomeiwen.com/subject/hxzcyktx.html