问题
I can't get the CMVideoFormatDescriptionGetCleanAperture() function to work. Using
var videoDescriptionRef = port.formatDescription as CMVideoFormatDescriptionRef
var cleanAperture = CMVideoFormatDescriptionGetCleanAperture(videoDescriptionRef, true)
or
var cleanAperture = CMVideoFormatDescriptionGetCleanAperture(port.formatDescription, true)
gives me the following errors, respectively:
Cannot convert expression's type 'CMVideoFormatDescriptionRef' to type 'CMVideoFormatDescriptionRef'
And the second is
Could not find an overlad for '__conversion' that accepts the supplied arguments
Does anyone know how to fix these or can anyone point out any errors in getting the format description for retrieving the clean aperture?
回答1:
CMFormatDescription itself does not have a member takeUnretainedValue. It is a member of Unmanaged<T>. You need to define an unmanaged variable first, get a value into it and then you retain it.
As I do not know how you created port.formatDescription, here is an example where formatDescription is created from PPS/SPS from an AVC video.
// create the receiving unmanaged variable
var formatDescription: Unmanaged<CMVideoFormatDescription>?
// call the not annotated method, this method will fill in formatDescription which is Unmanaged<T>
let result = CMVideoFormatDescriptionCreateFromH264ParameterSets(nil, UInt(parameterSets.count), parameterSets, parameterSetSizes, 4, &formatDescription)
// check result, yada yada yada before continuing
// now we can actually retain the value
let formatDescriptionRef = formatDescription!.takeUnretainedValue() as CMVideoFormatDescriptionRef
// and we can do what ever we want with it in Swift without caring anymore.
var cleanAperture = CMVideoFormatDescriptionGetCleanAperture(formatDescriptionRef, 1)
- Create an unmanaged type
- Use this unmanaged type in an not annotated method
- Retain the value
- Work with it without thinking about how you created it.
So adjust your code where you create the port.formatDescription as a Unmanaged and you should be good.
回答2:
Try to use .takeUnretainedValue() like this:
var videoDescription : CMVideoFormatDescription = port.formatDescription.takeUnretainedValue()
var cleanAperture = CMVideoFormatDescriptionGetCleanAperture(videoDescription, true)
来源:https://stackoverflow.com/questions/24395265/cmvideoformatdescriptiongetcleanaperture-swift-error