swift4 视频转gif

本文介绍了一种使用Swift编程语言将视频文件转换为GIF动画的方法。通过AVFoundation和CoreMedia框架,从指定视频中抽取帧并创建GIF文件。文章详细展示了如何设置帧率、循环次数等参数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//
//  ViewController.swift
//  CatCommunity
//
//  Created by kangtaier on 2017/10/23.
//  Copyright © 2017年 kangtaier. All rights reserved.
//

import UIKit
import AVFoundation
import CoreMedia

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        managerVideoToGif(frequency:5)
    }

    func managerVideoToGif(frequency:Int){
        //图片转GIF配置
        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"))//init(url: URL(string: "http://ongelo4u0.bkt.clouddn.com/15011427040376xWtn.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)
            //帧图片存入到destination
            CGImageDestinationAddImage(destination!, image.cgImage! , frameProperties)
        }
        CGImageDestinationSetProperties(destination!, gifProperties)
        //保存gif图像
        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)

    }
    */

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值