IOS - Convert UIImage to vImage memory

I have a function where I convert UIImageto vImage(for use in some methods vImagein iOS Accelerate.framework.

I have a method:

-(vImage_Buffer)convertImage:(UIImage *)image {
    CGImageRef sourceRef = [image CGImage];
    NSUInteger sourceWidth = CGImageGetWidth(sourceRef);
    NSUInteger sourceHeight = CGImageGetHeight(sourceRef);
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    unsigned char *sourceData = (unsigned char*)calloc(sourceHeight * sourceWidth * 4, sizeof(unsigned char));
    NSUInteger bytesPerPixel = 4;
    NSUInteger sourceBytesPerRow = bytesPerPixel * sourceWidth;
    NSUInteger bitsPerComponent = 8;
    CGContextRef context = CGBitmapContextCreate(sourceData, sourceWidth, sourceHeight,
                                                       bitsPerComponent, sourceBytesPerRow, colorSpace,
                                                       kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Big);
    CGContextDrawImage(context, CGRectMake(0, 0, sourceWidth, sourceHeight), sourceRef);
    CGContextRelease(context);
    vImage_Buffer v_image = {
        .data = sourceData,
        .height = sourceHeight,
        .width = sourceWidth,
        .rowBytes = sourceBytesPerRow
    };

    return v_image;
}

I built this by mixing and matching several pieces of code on the Internet.

My question is: I have a call callocthat allocates space for sourceData. But how and where will I free this memory?

, , , -, . , , .data vImage_Buffer. ? v_image nil ( , ) , calloc?

- ?

+4
2

colorSpace, CGColorSpaceRelease(colorSpace)

, calloc, free(v_image.data) ( , ).

, CGContext CGImage CGDataProviderRef, - :

-(vImage_Buffer)convertImage:(UIImage *)image
{
    CGImageRef sourceRef = [image CGImage];
    NSUInteger sourceWidth = CGImageGetWidth(sourceRef);
    NSUInteger sourceHeight = CGImageGetHeight(sourceRef);

    CGDataProviderRef provider = CGImageGetDataProvider(sourceRef);
    CFDataRef bitmapData = CGDataProviderCopyData(provider);

    unsigned char *sourceData = (unsigned char*)calloc(sourceHeight * sourceWidth * 4, sizeof(unsigned char));
    NSUInteger bytesPerPixel = 4;
    NSUInteger sourceBytesPerRow = bytesPerPixel * sourceWidth;

    CFDataGetBytes(bitmapData, CFRangeMake(0, CFDataGetLength(bitmapData)), sourceData);

    vImage_Buffer v_image = {
        .data = (void *)sourceData,
        .height = sourceHeight,
        .width = sourceWidth,
        .rowBytes = sourceBytesPerRow
    };

    CFRelease(bitmapData);

    return v_image;
}

.

- vImageCreateFromImage. , .

+3

-, , , iOS7.0/OSX 10.9 vImageBuffer_InitWithCGImage ( vImage_Utilities.h ). , .

-, InitWithCGImage, vImage_Buffer, , , : free(buffer.data);.

+1

Source: https://habr.com/ru/post/1535159/


All Articles