简体   繁体   English

从 UIImagePickerController 获取 UIImageView 中的 Gif

[英]Get Gif in UIImageView from UIImagePickerController

GIFU library using to display a Gif image in UIImageView picked from UIImagePickerController GIFU库用于在从UIImagePickerController中选取的UIImageView显示 Gif 图像

  public func imagePickerController(_ picker: UIImagePickerController,
                                    didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) {
    
    
    let imageUrl          = info[.referenceURL] as! NSURL
    let imageName         = imageUrl.lastPathComponent
    let documentDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!
    let photoURL          = NSURL(fileURLWithPath: documentDirectory)
    let localPath         = photoURL.appendingPathComponent(imageName!)
    let image             = info[.originalImage]as! UIImage
    let data              = image.jpegData(compressionQuality: 0)
    
    do {
      try data?.write(to: localPath!, options: Data.WritingOptions.atomic)
    }
    catch
    {
      // Catch exception here and act accordingly
    }
    self.pickerController(picker, didSelect: data)
  }
  
  func didSelect(image: Data?) {
    
    tempImage.prepareForAnimation(withGIFData: image!, loopCount: 3) {
      DispatchQueue.main.asyncAfter(deadline: .now() + 0.5, execute: {
        self.tempImage.startAnimating()
      })
    }
  }

Image displaying but not animating, looks image format gets changed after selecting.图像显示但不动画,选择后看起来图像格式变了。

You are converting GIF to jpeg data using the following line of code.您正在使用以下代码行将 GIF 转换为 jpeg 数据。

 let data  = image.jpegData(compressionQuality: 0)

Avoid data conversion to JPEG, now(I guess from iOS 13 onwards) native controller does have support for previewing gif file on selection try that.避免将数据转换为 JPEG,现在(我猜从 iOS 13 开始)本机控制器确实支持在选择时预览 gif 文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从UICollectionViewCell内的UIImageView呈现的UIImagePickerController - UIImagePickerController presented from UIImageView inside UICollectionViewCell 如何将数据从UIImagePickerController传递到UIImageView - How to pass data from a UIImagePickerController to a UIImageView UIImageView没有显示来自UIImagePickerController的PNG图像的透明度 - UIImageView not showing transparency of PNG Images from UIImagePickerController UIImageView不显示来自UIImagePickerController的图像 - UIImageView does not show Image from UIImagePickerController 如何使用Tap Gesture从UIImageView呈现UIImagePickerController - How to present UIImagePickerController from UIImageView with Tap Gesture 从 UIImagePickerController 获取 PHAsset - get PHAsset From UIImagePickerController Swift:可以获取UIImagePickerController使用的UIImageView的默认大小吗? - Swift: is possible to get the default size of UIImageView used by UIImagePickerController? UIImagePickerController不将图像保存到UIImageView - UIImagePickerController not saving image to UIImageView UIImagePickerController没有将图像分配给uiimageview - UIImagePickerController not assigning image to uiimageview 如何将图像从uiimagepickercontroller传递到另一个场景的uiimageview - How to pass image, from uiimagepickercontroller, into another scene's uiimageview
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM