Uiview到数据(将Gif保存到Firebase)

时间:2018-10-09 18:52:04

标签: swift firebase firebase-storage animated-gif

在我的应用程序中,我从相机拍摄照片,然后用户可以在其上添加gif贴纸。 我想将结果保存在Firebase Storage(以gif格式)

因此,总而言之,我想将UIview转换为Data,以将其保存在Firebase(Storage.storage().reference().child("Gif").putData(data, metadata: nil, completion: { (metadata, error) in })

我尝试过:

 let yourViewToData = NSKeyedArchiver.archivedData(withRootObject: self.canvasView)

但结果不是gif文件

我尝试过这样的功能,但是我无法截取数据,也不确定结果:

  let img = self.canvasView.toImage()


animatedGif(from: [img])

func animatedGif(from images: [UIImage]) {
        let fileProperties: CFDictionary = [kCGImagePropertyGIFDictionary as String: [kCGImagePropertyGIFLoopCount as String: 0]]  as CFDictionary
        let frameProperties: CFDictionary = [kCGImagePropertyGIFDictionary as String: [(kCGImagePropertyGIFDelayTime as String): 1.0]] as CFDictionary

        let documentsDirectoryURL: URL? = try? FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
        let fileURL: URL? = documentsDirectoryURL?.appendingPathComponent("animated.gif")

        if let url = fileURL as CFURL? {
            if let destination = CGImageDestinationCreateWithURL(url, kUTTypeGIF, images.count, nil) {
                CGImageDestinationSetProperties(destination, fileProperties)
                for image in images {
                    if let cgImage = image.cgImage {
                        CGImageDestinationAddImage(destination, cgImage, frameProperties)
                    }
                }
                if !CGImageDestinationFinalize(destination) {
                    print("Failed to finalize the image destination")
                }
                print("Url = \(fileURL)")
            }
        }
    }

有人知道如何将UIView保存到Firebase Storage上的gif文件吗?

我正在寻找一种替代解决方案。从我的UIView中获取UImage的15张屏幕截图,以便稍后制作gif。我有这个功能:

func toImage() -> UIImage {
    UIGraphicsBeginImageContextWithOptions(self.bounds.size, self.isOpaque, 0.0)
    self.drawHierarchy(in: self.bounds, afterScreenUpdates: false)
    let snapshotImageFromMyView = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return snapshotImageFromMyView!
}

有人知道如何使用此功能制作15张屏幕截图吗?谢谢

谢谢!

0 个答案:

没有答案