在 SKView 中显示纵横比适合的视频和图像

Display aspect fit Video and Image in SKView

我一直在努力使用 SKSpriteNodeSKView 中显示纵横比适合的图像。我遇到了很多问题,例如尺寸问题,无法放置特定来源的图像等。 同样的问题我不得不在 SKView.

中使用 SKVideoNode 显示视频
// Assign Image URL to SKTexture
  func PictureSection() {
     let sceneImageFeedTexture = SKTexture(image:url)
     let pictureBoxImage = SKSpriteNode(texture: sceneImageFeedTexture)
     pictureBoxImage.anchorPoint = CGPoint(x: 0, y: 0)
     pictureBoxImage.aspectFillToSizeForImage(fillSize: CGSize(width: size.width, height: size.height)) // Do this after you set texture
     let imageY = (skviewcontainerheight - pictureBoxImage.size.height)/2
     pictureBoxImage.position = CGPoint(x: (size.width-pictureBoxImage.size.width)/2, y: imageY)
     addChild(pictureBoxImage)
}
//Calulate Image size
extension SKSpriteNode {
func aspectFillToSizeForImage(fillSize: CGSize) {
    if let texture = self.texture {
        let horizontalRatio = fillSize.width / texture.size().width
        let verticalRatio = fillSize.height / texture.size().height
        let finalRatio = horizontalRatio < verticalRatio ? horizontalRatio : verticalRatio
        size = CGSize(width: texture.size().width * finalRatio, height: texture.size().height * finalRatio)
    }
}
}


//Assign Video
func VideoBox{
 let player2 = AVPlayer(url: url as URL)
 let videoNode = SKVideoNode(avPlayer: player2)
 videoNode.position = CGPoint(x: frame.size.width/2, y: frame.size.height/2)
 videoNode.size = resolutionForLocalVideo(url:url)!
 videoNode.size = finalVideoSize(skvideo: videoNode)
 addChild(videoNode)
 videoNode.play()
}
func resolutionForLocalVideo(url:URL)-> CGSize? {
    guard let track = AVURLAsset(url: url).tracks(withMediaType: AVMediaType.video).first else {return nil}
    let size = track.naturalSize.applying(track.preferredTransform)
     return CGSize(width: abs(size.width), height: abs(size.height))
 }

func finalVideoSize(skvideo:SKVideoNode)-> CGSize{
    let horizontalRatio = self.size.width / skvideo.size.width
    let verticalRatio = AppDelegate.shared.imageHeight! / skvideo.size.height
    let finalRatio = horizontalRatio < verticalRatio ? horizontalRatio : verticalRatio
    let size1 = CGSize(width: skvideo.size.width * finalRatio, height: skvideo.size.height * finalRatio)
    return size1
}