Попытка создать 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
}