如何从swift 4中的UIImagepicker中选择GIF图像?

时间:2018-03-20 04:15:26

标签: swift uiimageview ios11

在我的项目中尝试从UIImage选择器导入GIF。从图库中选取的GIF图像会执行某些操作,例如播放和停止。现在我可以从图库中选择GIF图像,但结果显示为图像而不是gif图像。

任何人都可以帮助我解决这个问题。

1 个答案:

答案 0 :(得分:1)

试试这会对你有帮助。

第1步:创建全局变量

var IMG:String?

第2步:将此功能放入您的代码中。

    func getFileName(info: [String : Any]) -> String {

    if let assetPath = info[UIImagePickerControllerReferenceURL] as? URL {
        let result = PHAsset.fetchAssets(withALAssetURLs: [assetPath], options: nil)
        let asset = result.firstObject
        let fileName = asset?.value(forKey: "filename")
        let fileUrl = URL(string: fileName as! String)
        if let name = fileUrl?.deletingPathExtension().lastPathComponent {
            print(name)


   //let assetPath = info[UIImagePickerControllerReferenceURL] as! NSURL
            if (assetPath.absoluteString.hasSuffix("JPG")) {
                sonu = "JPG"
                print("JPG")
            }
            else if (assetPath.absoluteString.hasSuffix("PNG")) {
                sonu = "PNG"
                print("PNG")
            }
            else if (assetPath.absoluteString.hasSuffix("GIF")) {
                sonu = "GIF"
                print("GIF")
            }
            else {
                sonu = "Unknown"
                print("Unknown")
            }

            return name
        }
    }
    return ""

}

第3步:在ImagePickerController

中设置此代码
    if let image = info[UIImagePickerControllerEditedImage] as? UIImage
    {
        imageview.image = image
        let IMG = getFileName(info: info)
        print(IMG)
        if IMG == "JPG"
        {
            imageDataForApi = UIImagePNGRepresentation(image)
        }
        else if IMG == "GIF"
        {
            imageDataForApi = UIImageJPEGRepresentation(image, 0)
        }
    }
    self.dismiss(animated: true) { () -> Void in

    }