【问题标题】:Retrived Image directly with file URL直接使用文件 URL 检索图像
【发布时间】:2017-11-29 00:31:55
【问题描述】:

我正在尝试使用文件 URL ("file:///var/mobile/Media/DCIM/100APPLE/IMG_0056.JPG") 获取图像,this method 效果很好,但没有办法制作它更好吗?因为检索我的图像需要很长时间。

我试过用这个方法直接获取带有url的图片:

if let url = URL(string: self.path) {
        print(url) (print = file:///var/mobile/Media/DCIM/100APPLE/IMG_0056.JPG)
        do {
            let data = try Data(contentsOf: url)
            print(data)
            let image2 = UIImage(data: data as Data)
            return image2
        } catch {
            print(error.localizedDescription)
            // (print = Impossible d’ouvrir le fichier « IMG_0056.JPG » car vous ne disposez pas de l’autorisation nécessaire pour l’afficher.)
            // Traduction : Unable to open file "IMG_0056.JPG" because you do not have permission to view it.
        }
    }

    return nil

如何直接访问此图像?

【问题讨论】:

    标签: image url path phasset


    【解决方案1】:

    我终于找到了一种让它更快的方法。我发现的唯一方法是将 PHAsset 对象的 localIdentifier 存储在我的对象中。

    现在,我的方法是这样的:

    func PHAssetForFileURL(url: NSURL) -> PHAsset? {
        let imageRequestOptions = PHImageRequestOptions()
        imageRequestOptions.version = .current
        imageRequestOptions.deliveryMode = .fastFormat
        imageRequestOptions.resizeMode = .fast
        imageRequestOptions.isSynchronous = true
    
        let options = PHFetchOptions()
        options.predicate = NSPredicate(format: "localIdentifier == %@", self.localIdentifier )
    
        let fetchResult = PHAsset.fetchAssets(with: options)
        for index in 0 ..< fetchResult.count {
            let asset = fetchResult[index] as PHAsset
            var found = false
            PHImageManager.default().requestImageData(for: asset, options: imageRequestOptions) { (_, _, _, info) in
                if let urlkey = info?["PHImageFileURLKey"] as? NSURL {
                    if urlkey.absoluteString! == url.absoluteString! {
                        found = true
                    }
                }
            }
            if (found) {
                return asset
    
            }
        }
    
        return nil
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-06-28
      • 2023-02-16
      • 2019-11-07
      • 2011-06-27
      • 2013-12-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多