使用UIGraphicsBeginImageContextWithOptions创建Gif时缺少框架

时间:2019-05-22 06:19:46

标签: ios swift gif uiviewanimation animated-gif

尝试通过截屏从动画中创建Gif,但是除第一次外,每次输出的gif都缺少帧。 尝试过UIGraphicsEndImageContext()来清除堆栈,但它不起作用。

截图功能

    private func screenShot(view: UIView) {

    UIGraphicsBeginImageContext(view.bounds.size)
    view.drawHierarchy(in: view.bounds, afterScreenUpdates: true)

    // And finally, get image
    var image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    if (image != nil)
    {
        self.frameList.append(image!)
        image = nil
    }
}

动画以截图

     private func animation1Screenshot() {
    isAnimationFinishedScreenshot = false
    self.view.layoutIfNeeded()
    UIView.animate(withDuration: 0.5, delay: 1, options: 
    UIView.AnimationOptions.curveEaseOut, animations: {

    } , completion: { finished in

        UIView.animate(withDuration: 0.5, delay: 1, options: UIView.AnimationOptions.curveEaseOut, animations: {
            self.uploadVideoView.simpleAnimChildViewCentreXConstraintScreenshot.constant += -self.uploadVideoView.anim1WidthConstraint.constant
            self.uploadVideoView.simpleViewScreenshot.layoutIfNeeded()
        }, completion: { finished in
            UIView.animate(withDuration:1, delay: 14 ,animations: {
                self.uploadVideoView.simpleViewScreenshot.alpha = 0.0
                self.uploadVideoView.simpleAnimChildViewScreenshot.alpha = 0.0
                self.uploadVideoView.simpleViewScreenshot.layoutIfNeeded()
            }, completion: { finished in
                if !self.videoCancelled {
                    DispatchQueue.main.asyncAfter(deadline: .now() + 15.0, execute: {
                        self.isAnimationFinishedScreenshot = true
                    })
                } else {
                    self.isAnimationFinishedScreenshot = true
                }
            })

        })
    })

}

生成GIF的函数

        public func generateGif(photos: [UIImage], filename: 
        String,gifheight : CGFloat) -> Bool {
        if let docsDirectory = getDocumentsDirectory() {
        let url = docsDirectory.appendingPathComponent(filename)

        //Utils.removeImageLocalPath(localPathName: url.path)
        let fileProperties = [kCGImagePropertyGIFDictionary as 
        String: [kCGImagePropertyGIFLoopCount as String: 0]]
        let gifProperties = [kCGImagePropertyGIFDictionary as String: [kCGImagePropertyGIFDelayTime as String: 0.125]]
        if let destination = CGImageDestinationCreateWithURL(url as CFURL, kUTTypeGIF, photos.count, nil) {
            CGImageDestinationSetProperties(destination, fileProperties as CFDictionary?)
            for photo in photos {
                let resizedImage = resizeImage(image: photo, targetSize: CGSize(width: photo.size.width, height: gifheight))
                if let image = resizedImage.cgImage {
                    CGImageDestinationAddImage(destination, image, gifProperties as CFDictionary?)
                } 

            }
            return CGImageDestinationFinalize(destination)
        }
    }
    return false
}

0 个答案:

没有答案