Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8500981
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T01:05:15+00:00 2026-06-11T01:05:15+00:00

I have a method that will add a filter to an image. This worked

  • 0

I have a method that will add a filter to an image. This worked fine until a couple of months ago, now when I try to use this method the application will crash on the images buffer. I create the buffer and set it to the image’s data, accessing the specific index later causes a bad access crash. I have looked for the past hour or two, and now I am convinced there is something im overlooking. I think something is being released that should not be. I am using the ios DP 4 preview of xcode, and I think this problem started with the update to the beta, but I am really not sure.

This is the line it crashes on located near the middle of the first for loop

    m_PixelBuf[index+2] = m_PixelBuf[index+2]/*aRed*/;

Normally it is set to aRed Which I have checked, and it should not go out of the buffers boundaries.

    -(void)contrastWithContrast:(float )contrast colorWithColor:(float )color{
    drawImage.image = original;
    UIImage * unfilteredImage2 = [[[UIImage alloc]initWithCGImage:drawImage.image.CGImage]    autorelease];
    CGImageRef inImage = unfilteredImage2.CGImage;         
    CGContextRef ctx;
    CFDataRef m_DataRef;
    m_DataRef = CGDataProviderCopyData(CGImageGetDataProvider(inImage));
    UInt8 * m_PixelBuf  = (UInt8 *) CFDataGetBytePtr(m_DataRef);
    int length = CFDataGetLength(m_DataRef);
    NSLog(@"Photo Length: %i",length);
    //////Contrast/////////////
    //NSLog(@"Contrast:%f",contrast);
    int aRed;
    int aGreen;
    int aBlue;
    for (int index = 0; index < length; index += 4){
    aRed = m_PixelBuf[index+2];
    aGreen = m_PixelBuf[index+1];
    aBlue = m_PixelBuf[index];

    aRed = (((aRed-128)*(contrast+100) )/100) + 128;
    if (aRed < 0) aRed = 0; if (aRed>255) aRed=255;
    m_PixelBuf[index+2] = m_PixelBuf[index+2]/*aRed*/;//Always crashes here

    aGreen = (((aGreen-128)*(contrast+100) )/100) + 128;
    if (aGreen < 0) aGreen = 0; if (aGreen>255) aGreen=255;
    m_PixelBuf[index+1] = aGreen;

    aBlue = (((aBlue-128)*(contrast+100) )/100) + 128;
    if (aBlue < 0) aBlue = 0; if (aBlue>255) aBlue=255;
    m_PixelBuf[index] = aBlue;
}
ctx = CGBitmapContextCreate(m_PixelBuf,
                            CGImageGetWidth( inImage ),  
                            CGImageGetHeight( inImage ),  
                            CGImageGetBitsPerComponent(inImage),
                            CGImageGetBytesPerRow(inImage ),  
                            CGImageGetColorSpace(inImage ),  
                            CGImageGetBitmapInfo(inImage) );
CGImageRef imageRef = CGBitmapContextCreateImage (ctx);
UIImage* rawImage = [[UIImage alloc]initWithCGImage:imageRef];
drawImage.image = rawImage;

[rawImage release];
CGContextRelease(ctx); 
CFRelease(imageRef);
CFRelease(m_DataRef);

unfilteredImage2 = [[[UIImage alloc]initWithCGImage:drawImage.image.CGImage] autorelease];
inImage = unfilteredImage2.CGImage;         
m_DataRef = CGDataProviderCopyData(CGImageGetDataProvider(inImage));
m_PixelBuf = (UInt8 *) CFDataGetBytePtr(m_DataRef);
length = CFDataGetLength(m_DataRef);

///////Color////////////////    
for (int index = 0; index < length; index += 4)
{
    //Blue
    if((m_PixelBuf[index] + ((int)color * 2))>255){
        m_PixelBuf[index] = 255;
    }else if((m_PixelBuf[index] + ((int)color * 2))<0){
        m_PixelBuf[index] = 0;
    }
    else{
        m_PixelBuf[index]=m_PixelBuf[index] + ((int)color * 2);
    }

    //Green
    if((m_PixelBuf[index+1] + ((int)color * 2))>255){
        m_PixelBuf[index+1] = 255;
    }else if((m_PixelBuf[index+1] + ((int)color * 2))<0){
        m_PixelBuf[index+1] = 0;             
    }
    else{
        m_PixelBuf[index+1]=m_PixelBuf[index+1] + ((int)color * 2);  
    }

    //Red
    if((m_PixelBuf[index+2] + ((int)color * 2))>255){
        m_PixelBuf[index+2] = 255;
    }else if((m_PixelBuf[index+2] + ((int)color * 2))<0){
        m_PixelBuf[index+2] = 0;             
    }
    else{
        m_PixelBuf[index+2]=m_PixelBuf[index+2] + ((int)color * 2);  
    }

    //m_PixelBuf[index+3]=255;//Alpha
}

ctx = CGBitmapContextCreate(m_PixelBuf,  
                            CGImageGetWidth( inImage ),  
                            CGImageGetHeight( inImage ),  
                            CGImageGetBitsPerComponent(inImage),
                            CGImageGetBytesPerRow(inImage ),  
                            CGImageGetColorSpace(inImage ),  
                            CGImageGetBitmapInfo(inImage) );
imageRef = CGBitmapContextCreateImage (ctx);
rawImage = [[UIImage alloc]initWithCGImage:imageRef];
drawImage.image = rawImage;
[rawImage release];
CGContextRelease(ctx); 
CFRelease(imageRef);
CFRelease(m_DataRef);
//drawImage.image = unfilteredImage2;
willUpdate = YES;
}

sorry for any extra comments/info I just copied the whole method in.

Thanks,

Storealutes

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T01:05:17+00:00Added an answer on June 11, 2026 at 1:05 am

    I had same problem.
    You should use below code to get pointer to pixel buffer instead of CFDataGetBytePtr().

    CGImageRef cgImage = originalImage.CGImage;
    size_t width = CGImageGetWidth(cgImage);
    size_t height = CGImageGetHeight(cgImage);
    
    char *buffer = (char*)malloc(sizeof(char) * width * height * 4);
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef cgContext = CGBitmapContextCreate(buffer, width, height, 8, width * 4, colorSpace, kCGImageAlphaPremultipliedLast);
    CGContextSetBlendMode(cgContext, kCGBlendModeCopy);
    CGContextDrawImage(cgContext, CGRectMake(0.0f, 0.0f, width, height), cgImage);
    
    free(buffer);
    CGContextRelease(cgContext);
    CGColorSpaceRelease(colorSpace);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a method with this signature, and another method that will be the
I have a method that will receive a string , but before I can
I have a method that I will use in the following contexts: 1. User
I have created a custom method that will return unique items, together with the
I have some code in method C that will get executed based upon the
I am trying to have a method that takes in a username and will
I'm creating a class that will have one public method, which returns a value
I'm trying to write an extension method that will add the function HasFactor to
I have a method that will return a list of suggested orders. If the
I have a controller method that looks like this: [AcceptGet] public ActionResult Index(SecurityMatrixIndexViewModel model)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.