首页 > 解决方案 > Uiview 到数据(将 Gif 保存到 firebase)

问题描述

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

总而言之,我想将我的 UIview 转换为数据,以将其保存在 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 中截取 15 张 UImage 截图,以便稍后制作 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 的截图吗?谢谢

谢谢 !

标签: swiftfirebasefirebase-storageanimated-gif

解决方案


推荐阅读