ARKitはどのようにSCNNodeにGifピクチャーを貼ります

8546 ワード

最近SCNNodeにGifピクチャをどのようにロードするかを研究し,2つの解決策をまとめた.まずSCNMaterialについて紹介します.SCNNodeの材質属性で、ノードに様々な皮膚材質を追加することができます.公式ドキュメントによると、SCNMaterialのcontentsはUIcolor、UIIImage、CALayer、NSURLなどを使用することができます.本当に無敵です.UIViewは言及していませんが、私自身が試験した後でもいいですが、GifをロードするとUIスレッドが詰まります.
Specifies the receiver's contents. This can be a color (NSColor, UIColor, CGColorRef), 
an image (NSImage, UIImage, CGImageRef), a layer (CALayer), a path (NSString or NSURL), 
a SpriteKit scene (SKScene), a texture (SKTexture, id or GLKTextureInfo), 
or a floating value between 0 and 1 (NSNumber) for metalness and roughness properties.

Gif画像をMP 4ファイルに変換すると
まずGifピクチャをMP 4ファイルに変換する必要があります.キャッシュに存在します.AVPlayerでロードできます.mp 4を転送するのに時間がかかります.バックグラウンドで前処理を行うことをお勧めします.
//        
let fileData:NSData = model.getFileData()
let tempUrl = URL(fileURLWithPath:NSTemporaryDirectory()).appendingPathComponent("temp.mp4")
GIF2MP4(data: fileData as Data)?.convertAndExport(to: tempUrl, completion: {
    let playerItem = AVPlayerItem.init(url: tempUrl)
    let player = AVPlayer.init(playerItem: playerItem)
    player.play()
              
    material.diffuse.contents = player
    })
}

func convertAndExport(to url :URL , completion: @escaping () -> Void ) {
        outputURL = url
        prepare()
        
        var index = 0
        var delay = 0.0 - gif.frameDurations[0]
        let queue = DispatchQueue(label: "mediaInputQueue")
        videoWriterInput.requestMediaDataWhenReady(on: queue) {
            var isFinished = true
            
            while index < self.gif.frames.count {
                if self.videoWriterInput.isReadyForMoreMediaData == false {
                    isFinished = false
                    break
                }
                
                if let cgImage = self.gif.getFrame(at: index) {
                    let frameDuration = self.gif.frameDurations[index]
                    delay += Double(frameDuration)
                    let presentationTime = CMTime(seconds: delay, preferredTimescale: 600)
                    let result = self.addImage(image: UIImage(cgImage: cgImage), withPresentationTime: presentationTime)
                    if result == false {
                        fatalError("addImage() failed")
                    } else {
                        index += 1
                    }
                }
            }
            
            if isFinished {
                self.videoWriterInput.markAsFinished()
                self.videoWriter.finishWriting() {
                    DispatchQueue.main.async {
                        completion()
                    }
                }
            } else {
                // Fall through. The closure will be called again when the writer is ready.
            }
        }
    }

二キーアニメーションによる実装
//  Gif    
let fileData = model.getFileData()
let animation : CAKeyframeAnimation = createGIFAnimationYY(data: fileData)

//  CALayer material                  
let layer = CALayer()
layer.bounds = CGRect(x: 0, y: 0, width: imageW, height: imageH)
layer.add(animation, forKey: "contents")

//   CALayer   material,Gif       1/4                
let tempView = UIView.init(frame: CGRect(x: 0, y: 0, width: imageW, height: imageH))
tempView.layer.bounds = CGRect(x: -imageW/2, y: -imageH/2, width: imageW, height: imageH)
tempView.layer.addSublayer(layer)
material.diffuse.contents = tempView.layer

キーフレームアニメーションに移行すると、Gifが大きく、フレーム数が多いため、メモリの最適化を行いました.極端なメモリ消費量は前の1/10に最適化されます.この方法は、前のロードよりも高速ですが、メモリ消費量はやや大きくなります.
func createGIFAnimationYY(data:NSData) -> CAKeyframeAnimation {
    
    let image = YYImage.init(data: data as Data)
    let bytes = Int.init(bitPattern: (image?.animatedImageBytesPerFrame())!)
    let nFrame = Int.init(bitPattern: (image?.animatedImageFrameCount())!)
    let bufferSize = Float(bytes*nFrame)
    let total = getDeviceMemoryTotal();
    let free = getDeviceFreeMemory();
    print("start: bufferSize:",bufferSize," totalMem: ",total," free: ",free)
    var maxSize = min(total*0.2, free!*0.6)
    maxSize = max(maxSize, Float(BUFFER_SIZE))
    if maxSize > bufferSize {
        maxSize = bufferSize
    }
    var maxBufferCount = Int(maxSize / Float(bytes))
    if maxBufferCount < 1 {
        maxBufferCount = 1
    } else if maxBufferCount > 256 {
        maxBufferCount = 256
    }
    //           
    let ratio = Float(nFrame)/Float(maxBufferCount)
    
    // Total loop time
    var time : Float = 0
    
    // Arrays
    var framesArray = [AnyObject]()
    var tempTimesArray = [NSNumber]()
    
    for i in 0..let curFrame = image?.animatedImageFrame(at: UInt(Float(i)*ratio))
        framesArray.append(curFrame!.cgImage!)
        var frameDuration = image?.animatedImageDuration(at: UInt(Float(i)*ratio))
        if frameDuration! - 0.011 < 0 {
            frameDuration = 0.100;
        }
        tempTimesArray.append(NSNumber(value: frameDuration!))
        print(frameDuration)
        time = time + Float(frameDuration!)
    }
    print("End: ",time)
    
    var timesArray = [NSNumber]()
    var base : Float = 0
    for duration in tempTimesArray {
        timesArray.append(NSNumber(value: base))
        base = base.advanced(by: duration.floatValue / time)
    }
    
    timesArray.append(NSNumber(value: 1.0))
    
    // Create animation
    let animation = CAKeyframeAnimation(keyPath: "contents")
    
    animation.beginTime = AVCoreAnimationBeginTimeAtZero
    animation.duration = CFTimeInterval(time)
    animation.repeatCount = Float.greatestFiniteMagnitude;
    animation.isRemovedOnCompletion = false
    animation.fillMode = kCAFillModeForwards
    animation.values = framesArray
    animation.keyTimes = timesArray
    animation.calculationMode = kCAAnimationDiscrete
    
    return animation;
}

効果図
テキストリンク:https://www.jianshu.com/p/f5d13540c08d
転載先:https://juejin.im/post/5ad86fc5f265da505f670f21