ios – 将Objective-C代码转换为Swift:可以省略释放调用吗?

ios – 将Objective-C代码转换为Swift:可以省略释放调用吗?,第1张

概述我们需要将以下代码从Objective-C转换为 Swift. 题: 有一些函数调用来释放对象,例如CGImageRelease(newImage).是否可以安全地假设Swift版本不需要模拟,因为所有内存管理都是自动的,或者你是否需要在Swift中释放内存? Objective-C代码: CVImageBufferRef imageBuffer = CMSampleBufferGetImageB 我们需要将以下代码从Objective-C转换为 Swift.

题:

有一些函数调用来释放对象,例如CGImageRelease(newImage).是否可以安全地假设Swift版本不需要模拟,因为所有内存管理都是自动的,或者你是否需要在Swift中释放内存?

Objective-C代码:

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版本:

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,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}
解决方法 根据文档:

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

所以你可以省略这些电话.

总结

以上是内存溢出为你收集整理的ios – 将Objective-C代码转换为Swift:可以省略释放调用吗?全部内容,希望文章能够帮你解决ios – 将Objective-C代码转换为Swift:可以省略释放调用吗?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1077700.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存