美文网首页
IOS 相册读取GIF数据

IOS 相册读取GIF数据

作者: 我是繁星 | 来源:发表于2018-08-19 21:24 被阅读0次

    本文讨论的只只针对于UIImagePickerControllerDelegate,本文是用swift写的,oc相同。
    由于iphone相册在ios11之前并不支持gif播放,但是其存储的是gif数据,但是如果选中的是gif数据,imagePicer方法返回的image只是gif的第一帧,那么如何获取到gif的真实数据呢?

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    }
    

    官方描述:
    Tells the delegate that the user picked a still image or movie.
    告诉代理方用户选择了图片或者视频。


    看下参数:info
    官方描述:
    A dictionary containing the original image and the edited image, if an image was picked; or a filesystem URL for the movie, if a movie was picked. The dictionary also contains any relevant editing information. The keys for this dictionary are listed in Editing Information Keys.
    如果选择了图像,则返回一个包含原始图像和已编辑图像的字典,如果选择了一个视频,则提供一个该视频的系统URL。


    由于ios11,对于info的内容作了一些修改,所以要根据系统区分对待。
    ios11:

    ["UIImagePickerControllerImageURL": [file:///Users/xingfan/Library/Developer/CoreSimulator/Devices/5ECD6F60-71F4-4172-ACC8-9EBE83C923B2/data/Containers/Data/Application/2D69E94A-6D1A-44DA-82BF-FA5271DB5190/tmp/1FCBE42D-E6B8-4844-9F19-24E062CB9CBB.gif](file:///Users/xingfan/Library/Developer/CoreSimulator/Devices/5ECD6F60-71F4-4172-ACC8-9EBE83C923B2/data/Containers/Data/Application/2D69E94A-6D1A-44DA-82BF-FA5271DB5190/tmp/1FCBE42D-E6B8-4844-9F19-24E062CB9CBB.gif),
     "UIImagePickerControllerMediaType": public.image, 
     "UIImagePickerControllerPHAsset": <PHAsset: 0x7fdd71f4bb90> 11E12104-2923-4F9E-B70E-3AAEF98C40B6/L0/001 mediaType=1/32, sourceType=1, (700x690), creationDate=2018-08-17 09:01:54 +0000, location=0, hidden=0, favorite=0 , 
     "UIImagePickerControllerReferenceURL": assets-[library://asset/asset.GIF?id=11E12104-2923-4F9E-B70E-3AAEF98C40B6&ext=GIF](library://asset/asset.GIF?id=11E12104-2923-4F9E-B70E-3AAEF98C40B6&ext=GIF),
     "UIImagePickerControllerOriginalImage": <UIImage: 0x6040002a8400> size {700, 690} orientation 0 scale 1.000000]
    

    ios8-10:

    ["UIImagePickerControllerMediaType": public.image,
     "UIImagePickerControllerReferenceURL": assets-[library://asset/asset.GIF?id=FDE27A54-A1C7-4C58-BA05-A75C27781D09&ext=GIF](library://asset/asset.GIF?id=FDE27A54-A1C7-4C58-BA05-A75C27781D09&ext=GIF), 
     "UIImagePickerControllerOriginalImage": <UIImage: 0x1740962b0> size {480, 800} orientation 0 scale 1.000000]
    

    这里的思路是可以用photos框架获取gif的PHAsset(可以理解为系统数据的描述,并不是实际数据),PHImageManager获取data数据,这里可以控制同步或者异步。而这个data就是gif数据。

            let options = PHImageRequestOptions()
            options.version = .current
            //异步
            options.isSynchronous = true
            PHImageManager.default().requestImageData(for: imageAsset, options: options) { (data, str, origin, info) in
                DispatchQueue.main.async(execute: {
                    self.myImage.gifImages = GifHelper.gifWithImages(gifData: data!)
                    self.backGroundImageView.animationImages = self.myImage.gifImages
                    self.backGroundImageView.animationDuration = 1
                    self.backGroundImageView.startAnimating()
                })
            }
    

    那么现在的问题就在如何获取到PHAsset实体,这里可以看到ios 11 中info直接返回了PHAsset实体,ios11之下的

    let imgUrl = info[UIImagePickerControllerReferenceURL] as? URL
    let imageAsset = PHAsset.fetchAssets(withALAssetURLs: [imgUrl!], options: nil).firstObject
    

    这里fetchAssetsWithALAssetURLs在ios11中被废弃了,完整代码如下

    //MARK:UIImagePickerControllerDelegate
        func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
            let type:String = (info[UIImagePickerControllerMediaType]as!String)
            //当选择的类型是图片
            print("\(info)")
            if type=="public.image"{
                let imgUrl:URL?
                let imageAsset:PHAsset?
                if #available(iOS 11.0, *) {
                    imgUrl = info[UIImagePickerControllerReferenceURL] as? URL
                    imageAsset = info[UIImagePickerControllerPHAsset] as? PHAsset
                } else {
                    imgUrl = info[UIImagePickerControllerReferenceURL] as? URL
                    imageAsset = PHAsset.fetchAssets(withALAssetURLs: [imgUrl!], options: nil).firstObject
                }
                let img = info[UIImagePickerControllerOriginalImage] as? UIImage
                if imgUrl!.absoluteString.lowercased().contains("gif"){
                    let options = PHImageRequestOptions()
                    options.version = .current
                    //异步
                    options.isSynchronous = true
                    PHImageManager.default().requestImageData(for: imageAsset, options: options) { (data, str, origin, info) in
                    DispatchQueue.main.async(execute: {
                    })
                  }
                }
            }
        }
    

    相关文章

      网友评论

          本文标题:IOS 相册读取GIF数据

          本文链接:https://www.haomeiwen.com/subject/trtqiftx.html