美文网首页
UIImage 取 RGBA , RBGA封装 UIImage

UIImage 取 RGBA , RBGA封装 UIImage

作者: rogerwu1228 | 来源:发表于2017-08-15 09:49 被阅读135次

    UIImage 2 RGBA data

    - (unsigned char *)getImagePixel
    {
        UIImage *image = [UIImage imageNamed:@"test_test.jpg"];
        CFDataRef pixelData = CGDataProviderCopyData(CGImageGetDataProvider(image.CGImage));
        const uint8_t* data = CFDataGetBytePtr(pixelData);
        
        int width = (int)image.size.width;
        int height = (int)image.size.height;
        
        size_t bitsPerPixel = CGImageGetBitsPerPixel(image.CGImage);
        printf("bitsPerPixel = %lu\n", bitsPerPixel);
        size_t gitsPerComponent = CGImageGetBitsPerComponent(image.CGImage);
        printf("gitsPerComponent = %lu\n", gitsPerComponent);
        
        uint8_t *imgData = (uint8_t *)malloc(width*height*4);
        memcpy(imgData, data, width*height*4);
        return imgData;
    }
    
    

    RGBA 2 UIImage

    void convertBitsDataIntoUIImage( uint32_t size , uint32_t width , uint32_t height , void*bitsData )
    {
        CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();//
        void *colorData = bitsData;
        CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, colorData, width * height * 4, NULL);
        
        CGImageRef cgImage2 = CGImageCreate(width,  //w
                                            height,  //h
                                            8,    //bitsPerComponent
                                            8 * 4,  //bitsPerPixel
                                            width*4,  //bytesPerRow
                                            colorSpace,
                                            kCGImageAlphaPremultipliedLast|kCGBitmapByteOrderDefault,
                                            provider,
                                            NULL,
                                            NO,
                                            kCGRenderingIntentDefault);
        UIImage *image = [UIImage imageWithCGImage:cgImage2];
        NSLog(@"-------%@",image);
        
        CGDataProviderRelease(provider);
        CGColorSpaceRelease(colorSpace);
        CGImageRelease(cgImage2);
    }
    

    相关文章

      网友评论

          本文标题:UIImage 取 RGBA , RBGA封装 UIImage

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