iosobjective-ccgimageref

Bad receiver type 'CGImageRef' (aka 'CGImage *')


I am an iOS beginner and was following How can I manipulate the pixel values in a CGImageRef in Xcode to learn altering CGImages. I changed the code a little so that the middle pixel in the image should get painted red instead of swapping blue and red buffers for each pixel.

But now I get the "Bad receiver type 'CGImageRef' (aka 'CGImage *')" error on sending the message with

    manipulated = [imageRef colorMiddle];

in this function:

    - (void)renderColorFrame:(CMSampleBufferRef)sampleBuffer
     {

 CVImageBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
CVPixelBufferLockBaseAddress(pixelBuffer, 0);

size_t cols = CVPixelBufferGetWidth(pixelBuffer);
size_t rows = CVPixelBufferGetHeight(pixelBuffer);
    
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

unsigned char *ptr = (unsigned char *) CVPixelBufferGetBaseAddressOfPlane(pixelBuffer, 0);

NSData *data = [[NSData alloc] initWithBytes:ptr length:rows*cols*4];
CVPixelBufferUnlockBaseAddress(pixelBuffer, 0);

CGBitmapInfo bitmapInfo;
bitmapInfo = (CGBitmapInfo)kCGImageAlphaNoneSkipFirst;
bitmapInfo |= kCGBitmapByteOrder32Little;

CGDataProviderRef provider = CGDataProviderCreateWithCFData((CFDataRef)data);

CGImageRef imageRef = CGImageCreate(cols,
                                    rows,
                                    8,
                                    8 * 4,
                                    cols*4,
                                    colorSpace,
                                    bitmapInfo,
                                    provider,
                                    NULL,
                                    false,
                                    kCGRenderingIntentDefault);



//hier image ref verarbeiten!!
manipulated = [imageRef colorMiddle]; //here

leftImage = CGImageCreateWithImageInRect(imageRef, CGRectMake(0, 0, self.view.frame.size.width * 6/7, self.view.frame.size.height));

rightImage = CGImageCreateWithImageInRect(imageRef, CGRectMake(self.view.frame.size.width / 7, 0, self.view.frame.size.width * 6/7, self.view.frame.size.height));

    //left image
_colorImageViewL.image = [[UIImage alloc] initWithCGImage:leftImage/*imageRef*/];
    
    //right image
_colorImageViewR.image = [[UIImage alloc]initWithCGImage:rightImage/*imageRef*/];

//Full
 //  _colorImageViewFull.image = [[UIImage alloc]initWithCGImage:imageRef];


CGImageRelease(imageRef);
CGImageRelease(leftImage);
CGImageRelease(rightImage);
CGDataProviderRelease(provider);
CGColorSpaceRelease(colorSpace);

}

I don't understand why I get this error here, because the message should be send with matching arguments:

        @interface ViewController () <AVCaptureVideoDataOutputSampleBufferDelegate> {
        
        STSensorController *_sensorController;
        
        AVCaptureSession *_avCaptureSession;
        AVCaptureDevice *_videoDevice;
    
        UIImageView *_depthImageView;
        //UIImageView *_depthImageView2;
        //UIImageView *_normalsImageView;
        
        //Left
        UIImageView *_colorImageViewL;
        
        //right
        UIImageView *_colorImageViewR;
        
        //Full
        //UIImageView *_colorImageViewFull;
        
        uint16_t *_linearizeBuffer;
        uint8_t *_coloredDepthBuffer;
        uint8_t *_normalsBuffer;
        
        STNormalEstimator *_normalsEstimator;
        
        UILabel* _statusLabel;
        
        GLKMatrix4 _projection;
        
        CGImageRef leftImage;
        CGImageRef rightImage;
        CGImageRef manipulated;
        
       
        
        
        AppStatus _appStatus;

    }
    
    - (BOOL)connectAndStartStreaming;
    - (void)renderDepthFrame:(STDepthFrame*)depthFrame;
    - (void)renderNormalsFrame:(STDepthFrame*)normalsFrame;
    - (void)renderColorFrame:(CMSampleBufferRef)sampleBuffer;
    - (void)setupColorCamera;
    - (void)startColorCamera;
    - (void)stopColorCamera;
    - (CGImageRef)colorMiddle:(CGImageRef)image; //here

@end 

Does anyone know what causes the error and how to fix this? I just can't think of anything, because everything else where the CGImageRefs are used works just fine, and as far as i know this should be the right way of doing so.

Please look over the bad looks of this question since i still need to learn how to format everything right.

Thanks in advance!


Solution

  • Your line:

    manipulated = [imageRef colorMiddle];
    

    needs to be:

    manipulated = [self colorMiddle:imageRef];
    

    colorMiddle: is a method on your ViewController class.