import UIKit
import AVFoundation
import CoreMedia
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
managerVideoToGif(frequency:5)
}
func managerVideoToGif(frequency:Int){
let myDirectory:String = NSHomeDirectory() + "/Documents"
let path:String = myDirectory + "/my.gif"
let pathUrl = URL.init(fileURLWithPath: path)
let cfPath = pathUrl as CFURL
let cfStr = "com.compuserve.gif" as CFString
let frameProperties = NSDictionary.init(object: NSDictionary(object: 0.2, forKey: kCGImagePropertyGIFDelayTime as NSString), forKey: kCGImagePropertyGIFDictionary as NSString)
let gifProperties = NSDictionary.init(object: NSDictionary(object: 1, forKey: kCGImagePropertyGIFLoopCount as NSString), forKey: kCGImagePropertyGIFDictionary as NSString)
var destination = CGImageDestinationCreateWithURL(cfPath,cfStr, Int.max, nil)
let asset = AVURLAsset.init(url: URL.init(fileURLWithPath: "/Users/kangtaier/Desktop/CatCommunity/Resources/1.mp4"))
let assetImageGenerator = AVAssetImageGenerator.init(asset: asset)
assetImageGenerator.appliesPreferredTrackTransform = true
assetImageGenerator.apertureMode = AVAssetImageGeneratorApertureMode.encodedPixels
let cmTimeDuration = assetImageGenerator.asset.duration as CMTime
let cmTimeSecond = Int(cmTimeDuration.seconds)
var cmTimeCurrent:Float64 = 0
for _ in 0..<Int.max {
if cmTimeCurrent >= Float64(cmTimeSecond){
break
}
let tmpTime = CMTimeMakeWithSeconds(cmTimeCurrent, cmTimeDuration.timescale)
let cgImage = try? assetImageGenerator.copyCGImage(at: tmpTime, actualTime: nil)
let image = UIImage.init(cgImage: cgImage!)
cmTimeCurrent += Float64(1)
print(tmpTime)
CGImageDestinationAddImage(destination!, image.cgImage! , frameProperties)
}
CGImageDestinationSetProperties(destination!, gifProperties)
let flag = CGImageDestinationFinalize(destination!)
destination = nil
print(path)
print(flag)
}
/*
func saveToFile(image:UIImage,index:Int){
let myDirectory:String = NSHomeDirectory() + "/Documents"
let path:String = myDirectory + "/\(index).png"
print(path)
let data = UIImagePNGRepresentation(image) as NSData?
data?.write(toFile: path, atomically: true)
}
func saveToGif(images:Array<UIImage>){
let myDirectory:String = NSHomeDirectory() + "/Documents"
let path:String = myDirectory + "/my.gif"
let pathUrl = URL.init(fileURLWithPath: path)
let cfPath = pathUrl as CFURL
let cfStr = "com.compuserve.gif" as CFString
var destination = CGImageDestinationCreateWithURL(cfPath,cfStr, images.count, nil)
let frameProperties = NSDictionary.init(object: NSDictionary(object: 0.2, forKey: kCGImagePropertyGIFDelayTime as NSString), forKey: kCGImagePropertyGIFDictionary as NSString)
let gifProperties = NSDictionary.init(object: NSDictionary(object: 1, forKey: kCGImagePropertyGIFLoopCount as NSString), forKey: kCGImagePropertyGIFDictionary as NSString)
for image in images {
CGImageDestinationAddImage(destination!, image.cgImage! , frameProperties)
}
CGImageDestinationSetProperties(destination!, gifProperties)
let flag = CGImageDestinationFinalize(destination!)
destination = nil
print(path)
print(flag)
}
*/
}