Creating Image on iOS and Attempting to assign image to UIImageView Causes EXC_BAD_ACCESS

StackOverflow https://stackoverflow.com/questions/21917234

  •  14-10-2022
  •  | 
  •  

문제

I am trying to create an image by going over each pixel in a buffer and assigning it a color based on the character in a const char *, and when I try to add the image to an image view, get an EXC_BAD_ACCESS error. Please help.

+ (UIImage *)imageEncryptingString:(NSString *)string{
    NSUInteger width = string.length;
    NSUInteger height = string.length;
    NSInteger dataLength = width * height * 4;
    UInt8 *data = (UInt8*)malloc(dataLength * sizeof(UInt8));
    const char * cryptoString = [string UTF8String];
    float red   = 0.0f;
    float green = 0.5f;
    float blue  = 0.5f;
    float alpha = 1.0f;
    for (int j=0; j<height; j++) {
        red = 1.0 / cryptoString[j];
        for (int i=0; i<width; i++) {


            int index = 4*(i+j*width);
            data[index]  =255*red;
            data[++index]=255*green;
            data[++index]=255*blue;
            data[++index]=255*alpha;

        }
    }

        // Create a CGImage with the pixel data
    CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, data, dataLength, NULL);
    CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
    CGImageRef image = CGImageCreate(width, height, 8, 32, width * 4, colorspace, kCGBitmapByteOrder32Big | kCGImageAlphaPremultipliedLast,

                                     provider, NULL, true, kCGRenderingIntentDefault);

        //Clean up
    CGColorSpaceRelease(colorspace);
    CGDataProviderRelease(provider);
        // Don't forget to free(data) when you are done with the CGImage

    UIImage *img = [UIImage imageWithCGImage:image];
    free(image);
    free(data);
    return img;
}

This is the code I am using to create the image (It is a category on UIImage)

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    UIImage *image = [UIImage imageEncryptingString:@"Hello World How are you doing?"];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    [self.view addSubview:imageView];
    [self.view layoutSubviews];
}

This is the code I am trying to use to create the image view.

And this is the debugger output I am able to get:

(lldb) po image.CGImage
error: Execution was interrupted, reason: EXC_BAD_ACCESS (code=1, address=0x6353946e).
The process has been returned to the state before expression evaluation.
(lldb) p image.CGImage
error: Execution was interrupted, reason: EXC_BAD_ACCESS (code=1, address=0x6353946e).
The process has been returned to the state before expression evaluation.
(lldb) 

The program will not even crash while running connected to the debugger, It just continues to highlight the line UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

도움이 되었습니까?

해결책

Try calling CFRelease(image) on your CGImageRef instead of free in your imageEncryptingString: method

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top