【问题标题】:How to load animated GIF from photo library如何从照片库加载动画 GIF
【发布时间】:2016-06-21 02:36:25
【问题描述】:

在 iOS Swift 中,我很难将设备照片库中的动画 GIF 加载到 UIImageView 或从中创建 .gif 文件。如果动画 GIF 是服务器上的文件或者它就在应用程序中,我可以毫无问题地显示动画 GIF。但是,当我从照片库中加载它时,我会丢失动画。我找到了一些 Objective-C 解决方案并尝试将它们转换为 Swift,但没有任何运气。

这是我目前所拥有的:

@IBAction func buttonTapped(sender: UIButton) {
    selectPhoto()
}

func selectPhoto() {
    let photoLibraryController = UIImagePickerController()
    photoLibraryController.delegate = self
    photoLibraryController.sourceType = UIImagePickerControllerSourceType.PhotoLibrary

    let mediaTypes:[String] = [kUTTypeImage as String]
    photoLibraryController.mediaTypes = mediaTypes
    photoLibraryController.allowsEditing = false

    self.presentViewController(photoLibraryController, animated: true, completion: nil)
}

// UIImagePickerControllerDelegate
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
    let origImage = info[UIImagePickerControllerOriginalImage] as! UIImage

    self.imageView.image = origImage

    picker.dismissViewControllerAnimated(true, completion: nil)

}

最理想的情况是,我想将照片库图像保存到服务器上的 gif 文件中。从那里我可以毫不费力地展示它。但是,我需要一些帮助才能将照片库中的数据转换为某种类型的属性,而不会丢失动画。我需要为此使用 ALAssetsLibrary 之类的东西吗?

谢谢。

【问题讨论】:

  • 这是一种方法--> github.com/kaishin/Gifu。最简单的方法是使用 CocoaPods 安装它。以下是如何使用 CocoaPods--> raywenderlich.com/97014/use-cocoapods-with-swift。这是 ObjC 中的另一个解决方案--> stackoverflow.com/a/4386709/4475605
  • 谢谢,@Adrian。我实际上已经设置了 Gifu 并且正在尝试它。它工作正常,只是它希望将图像编译到应用程序中。它不提供任何方法来从应用程序中的其他位置获取图像。因此,我无法让它与照片库中的图像一起使用。
  • 可以阅读我的博客文章,了解一个处理内存使用和从磁盘加载的 GIF 加载库:modejong.com/blog/post6_new_animated_gif_decoder_for_avanimator
  • 您知道使用设备的照片库作为来源的任何方法吗?我对它们作为文件或项目资源没有任何问题。如果不手动操作,我无法将它们从库中获取到文件中。

标签: ios swift uiimagepickercontroller gif animated-gif


【解决方案1】:

是的,使用 ALAssetsLibrary → 现在称为 PHAsset。

您应该获取 gif 的 NSData,而不是 UIImage(因为 UIImage 只会获取第一帧。)

所以基本上你会做这样的事情:

一个你得到的资产

let requestOptions = PHImageRequestOptions()
requestOptions.isSynchronous = true // adjust the parameters as you wish    

PHImageManager.default().requestImageData(for: asset, options: requestOptions, resultHandler: { (imageData, UTI, _, _) in
    if let uti = UTI,let data = imageData ,
        // you can also use UTI to make sure it's a gif
       UTTypeConformsTo(uti as CFString, kUTTypeGIF) {
        // save data here
    }
})      

资源:PHAsset

【讨论】:

  • 什么是资产?如何获取资产对象?
  • 请告诉什么是assist和requestOptions,?我们应该如何得到那个?
【解决方案2】:

您可以使用 InfoKey .imageURL 访问图像文件的位置并从该 URL 检索数据

// UIImagePickerControllerDelegate
    
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
    if let path = info[.imageURL] as? URL {
        if let data = try? Data(contentsOf: path) {
           self.imageView.image = UIImage(data: data)
                    
//Now you have Data of a file: so you can convert it to image or gif or send to server or start animation (e.g. Framework JellyGIF)
           self.imageView.startGif(with: .data(data))
                }
                  picker.dismissViewControllerAnimated(true, completion: nil)
                    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-24
    • 1970-01-01
    • 1970-01-01
    • 2020-01-29
    • 2013-11-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多