Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting Objective-C code to Swift: okay to omit release calls?

We need to convert the code below from Objective-C to Swift.

Question:

There are a few function calls to release objects, e.g., CGImageRelease(newImage). Is it safe to assume no analog is needed for the Swift version since all the memory management is automatic, or do you need to free up memory in Swift as well?

Objective-C code:

CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(imageSampleBuffer);
CVPixelBufferLockBaseAddress(imageBuffer,0);
uint8_t *baseAddress = (uint8_t *)CVPixelBufferGetBaseAddress(imageBuffer);
size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer);
size_t width = CVPixelBufferGetWidth(imageBuffer);
size_t height = CVPixelBufferGetHeight(imageBuffer);
CVPixelBufferUnlockBaseAddress(imageBuffer,0);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef newContext = CGBitmapContextCreate(baseAddress, width, height, 8, bytesPerRow, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst);
CGImageRef newImage = CGBitmapContextCreateImage(newContext); CGContextRelease(newContext);
CGColorSpaceRelease(colorSpace);
UIImage *image= [UIImage imageWithCGImage:newImage scale:1.0 orientation:orientation];
CGImageRelease(newImage);

Swift version so far:

private func turnBufferToPNGImage(imageSampleBuffer: CMSampleBufferRef, scale: CGFloat) -> UIImage {
    let imageBuffer = CMSampleBufferGetImageBuffer(imageSampleBuffer)

    // Lock base address
    CVPixelBufferLockBaseAddress(imageBuffer, 0)

    // Set properties for CGBitmapContext
    let pixelData = CVPixelBufferGetBaseAddress(imageBuffer)
    let width = CVPixelBufferGetWidth(imageBuffer)
    let height = CVPixelBufferGetHeight(imageBuffer)
    let bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer)
    let colorSpace = CGColorSpaceCreateDeviceRGB()

    // Create CGBitmapContext
    let newContext = CGBitmapContextCreate(pixelData, width, height, 8, bytesPerRow, colorSpace, CGImageAlphaInfo.PremultipliedFirst.rawValue)

    // Create image from context
    let rawImage = CGBitmapContextCreateImage(newContext)!
    let newImage = UIImage(CGImage: rawImage, scale: scale, orientation: .Up)

    // Unlock base address
    CVPixelBufferUnlockBaseAddress(imageBuffer,0)

    // Return image
    return newImage
}
like image 870
Crashalot Avatar asked Dec 25 '22 07:12

Crashalot


2 Answers

Per the docs:

Core Foundation types are automatically imported as full-fledged Swift classes. Wherever memory management annotations have been provided, Swift automatically manages the memory of Core Foundation objects, including Core Foundation objects that you instantiate yourself

So you can omit the calls.

like image 133
Ben Pious Avatar answered Dec 28 '22 09:12

Ben Pious


No You don't need release Core Foundation object because Apple said:

The Core Foundation CFTypeRef type completely remaps to the AnyObject type.

And

Core Foundation objects returned from annotated APIs are automatically memory managed in Swift—you do not need to invoke the CFRetain, CFRelease, or CFAutorelease functions yourself.

document here

like image 35
larva Avatar answered Dec 28 '22 09:12

larva