Search code examples
iosswiftxcodeswiftui

Swift AVAsset naturalSize error: Type of expression is ambiguous without a type annotation


let vidWidth = asset?.naturalSize()?.width
let vidHeight = asset?.naturalSize()?.height

I'm getting:

Type of expression is ambiguous without a type annotation

How to do this correctly?

What I've tried:

let vidWidth: CGFloat = asset?.naturalSize()?.width
let vidHeight: CGFloat = asset?.naturalSize()?.height

but I'm still getting the same error


Solution

  • As stated Here naturalSize is deprectaed from AVAsset , you need to use load(.naturalSize) of AVAssetTrack

    Task {
        do {
            let asset:AVAsset? = nil // For testing purposes
            let size = try await asset?.load(.tracks).first?.load(.naturalSize)
            print(size?.width)
            print(size?.height)
        }
        catch {
            print(error)
        }
    }