What's the right memory management pattern for buffer->CGImageRef->UIImage?

后端 未结 4 1861
Happy的楠姐
Happy的楠姐 2021-02-07 14:11

I have a function that takes some bitmap data and returns a UIImage * from it. It looks something like so:

UIImage * makeAnImage() 
{
    unsigned char * pixels          


        
4条回答
  •  离开以前
    2021-02-07 14:45

    unsigned char * pixels = malloc(...);
    

    I also had problem with malloc/free after using CGImageCreate I finally found good and simple solution. I just replace line:

    CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, pixels, pixelBufferSize, NULL);
    

    with:

    NSData *data = [NSData dataWithBytes:pixels length:pixelBufferSize];
    CGDataProviderRef provider = CGDataProviderCreateWithCFData((CFDataRef)data);
    

    Just after that i could free mallocked memory:

    free (pixels);
    

提交回复
热议问题