直接用文件检索图像 URL
Retrived Image directly with file URL
我正在尝试使用文件 URL ("file:///var/mobile/Media/DCIM/100APPLE/IMG_0056.JPG") 获取图像, 效果很好,但没有办法让它变得更好?因为检索我的图像需要很长时间。
我试过用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
我怎样才能直接访问这张图片?
我终于找到了一种让它更快的方法。我发现的唯一方法是在我的对象中存储 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
}
我正在尝试使用文件 URL ("file:///var/mobile/Media/DCIM/100APPLE/IMG_0056.JPG") 获取图像,
我试过用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
我怎样才能直接访问这张图片?
我终于找到了一种让它更快的方法。我发现的唯一方法是在我的对象中存储 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
}