0

I used the following method to convert the input image to grayscale and threshold:

UIImage *image = self.imageView.image;

    // Create image rectangle with current image width/height

    CGRect imageRect = CGRectMake(0, 0, image.size.width, image.size.height);
    NSLog(@"width %f, height %f", image.size.width, image.size.height  );

    // Grayscale color space
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();

    // Create bitmap content with current image size and grayscale colorspace
    CGContextRef context = CGBitmapContextCreate(nil, image.size.width, image.size.height, 8, 0, colorSpace, kCGImageAlphaNone);

    // Draw image into current context, with specified rectangle
    // using previously defined context (with grayscale colorspace)
    CGContextDrawImage(context, imageRect, [image CGImage]);

    // Create bitmap image info from pixel data in current context
    CGImageRef imageRef = CGBitmapContextCreateImage(context);

    // Create a new UIImage object
    UIImage *newImage = [UIImage imageWithCGImage:imageRef];

    // Release colorspace, context and bitmap information
    CGColorSpaceRelease(colorSpace);
    CGContextRelease(context);
    CFRelease(imageRef);

    // Return the new grayscale image
    self.imageView.image= newImage;




    //Thresholds the grayscale image



    CGImageRef sourceImage = newImage.CGImage ; //creates a CGImage reference for it

    //
    CFDataRef theData; //creates a variable of CFDataRef to store data of the image.
    //
    //
    //
    theData = CGDataProviderCopyData(CGImageGetDataProvider(sourceImage)); //assigns theData variable to the actual image
    //
    //
    //
    //
    //
    //
    //
    //
    UInt8 *pixelData = (UInt8 *) CFDataGetBytePtr(theData);
    //

    int dataLength = CFDataGetLength(theData);
    int counter=0;


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

        if (pixelData[index]  < 180)
        {
               NSLog(@"The intensity is %u", pixelData[index]);
            pixelData[index] = 0;
            //pixelData[index+1] = 0;
            //pixelData[index+2] = 0;
            //pixelData[index+3] = 0;

        }

        else
        {
            NSLog(@"The intensity is %u", pixelData[index]);
            pixelData[index] = 255;
           // pixelData[index+1] = 255;
           // pixelData[index+2] = 0;
            //pixelData[index+3] = 0;
        }





    }

The app crashes when the for loop is trying to rewrite the intensities here:

pixelData[index] = 0;

Could someone please help me out please?

Thanks!

1 Answer 1

1

CFDataGetBytePtr returns read-only pointer, as Apple docs say. A solution is proposed here.

Sign up to request clarification or add additional context in comments.

1 Comment

Your a legend mate! Thank you very much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.