swift - 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 的截图吗?谢谢
谢谢 !
解决方案
推荐阅读
- .htaccess - 如何在我的主页的搜索控制台中解决 301 重定向
- azure - 使用 Azure 存储创建外部表
- windows - 解释从网站复制的文本字符及其格式
- java - 在 Java 中克隆二维数组
- c# - 无法在 WindowsForm 继承上调整 UI 组件的大小 (Visual Studio 2015)
- ruby-on-rails - 如何在 JS 框中显示带有 Active Storage 的图像变体?
- javascript - Lightbox/iframe 中的 Worldpay Javascript API 托管集成不起作用
- ios - 在 NSMutableAttributedString 的子类中调用 append 时应用程序崩溃
- reactjs - 更改所选选项卡的图标颜色 - react-native-router-flux
- devextreme-angular - 以自定义语言本地化 devextreme dx-date-box Angular 2+