Question

I am retrieving data from an Image using the CGDataProviderCopyData but the bytes returned by this function is very large when compared to the image file size. Here is my code.

UIImage *image = self.imageView.image;
CGImageRef cgimage = image.CGImage;
CGDataProviderRef provider = CGImageGetDataProvider(cgimage);
NSData* data = (__bridge_transfer NSData*)CGDataProviderCopyData(provider);

Is there any other approach to read pixel data and obtain the rgba values from the image.

Was it helpful?

Solution 2

What you are doing is correct. The image file on disk is in a compressed format. When you load the data into a UIImage, the image is uncompressed and takes "width x height x 4" bytes. The "4" assumes RGBA. And actually the data can be a bit bigger since the bytes per row is usually a multiple of 16 bytes.

One thing about loading the bytes from the image. Don't assume it is RGBA. Depending on the image, it could be in other formats. Use the proper functions to determine the color model, bytes per pixel and bytes per row.

OTHER TIPS

hi this is way to find the values...

 CGImageRef imgSource=self.duplicateImage.image.CGImage;
    CFDataRef m_DataRef1 = CGDataProviderCopyData(CGImageGetDataProvider(imgSource)); 
    UInt8 *dataOriginal=(UInt8 *)CFDataGetBytePtr(m_DataRef1);
    double lengthSource=CFDataGetLength(m_DataRef1);
    NSLog(@"length::%f",lengthSource);

below one is the example of modifying the values...

   -(UIImage*)customBlackFilterOriginal
{
    CGImageRef imgSource=self.duplicateImage.image.CGImage;
    CFDataRef m_DataRef1 = CGDataProviderCopyData(CGImageGetDataProvider(imgSource)); 
    UInt8 *dataOriginal=(UInt8 *)CFDataGetBytePtr(m_DataRef1);
    double lengthSource=CFDataGetLength(m_DataRef1);
    NSLog(@"length::%f",lengthSource);
    int redPixel;
    int greenPixel;
    int bluePixel;

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

        dataOriginal[index]=dataOriginal[index];
        dataOriginal[index+1]= 101;
        dataOriginal[index+2]= 63;
        dataOriginal[index+3]=43;      

    } 

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

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

    CGColorSpaceRef colorspace=CGImageGetColorSpace(imgSource);
    CGBitmapInfo bitmapInfo=CGImageGetBitmapInfo(imgSource);
    CFDataRef newData=CFDataCreate(NULL,dataOriginal,lengthSource);
    CGDataProviderRef provider=CGDataProviderCreateWithCFData(newData);
    CGImageRef newImg=CGImageCreate(width,height,bitsPerComponent,bitsPerPixel,bytesPerRow,colorspace,bitmapInfo,provider,NULL,true,kCGRenderingIntentDefault);

    return [UIImage imageWithCGImage:newImg];

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top