iOS 照片库 - 如何获得真实的文件名?
iOS Photos library - how to get the REAL filename?
我正在为此寻找 Swift 或 Objective C 解决方案。在这里,我使用 swift 来展示我的例子。
我在 iOS 上使用照片框架,但很难找到真正的文件名而不是 IMG_4227.JPG
类型。我的图片名称实际上是 myawesomefilename.jpg
,如果我空投或将我的 iPhone 中的图片传输到保管箱,我会看到这个文件名。但是当我使用照片框架时,我得到的名称是 IMG_4227.JPG
.
app store上有一款app可以获取所有图片的真实文件名,绝对可以。我就是不知道怎么办。
这是我目前获取 IMG_4227.JPG
类型名称的方式:
func exifTapped(asset: PHAsset) {
print("name: \(asset.value(forKey: "filename"))")
getURL(ofPhotoWith: asset) { (url) in
print("URL: \(url)")
let data = NSData.init(contentsOf: url!)!
if let imageSource = CGImageSourceCreateWithData(data, nil) {
let imageProperties2 = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, nil)! as NSDictionary
print("imageProperties2: ", imageProperties2)
}
}
}
func getURL(ofPhotoWith mPhasset: PHAsset, completionHandler : @escaping ((_ responseURL : URL?) -> Void)) {
if mPhasset.mediaType == .image {
let options: PHContentEditingInputRequestOptions = PHContentEditingInputRequestOptions()
options.isNetworkAccessAllowed = true
options.canHandleAdjustmentData = {(adjustmeta: PHAdjustmentData) -> Bool in
return true
}
mPhasset.requestContentEditingInput(with: options, completionHandler: { (contentEditingInput, info) in
completionHandler(contentEditingInput!.fullSizeImageURL)
})
} else if mPhasset.mediaType == .video {
let options: PHVideoRequestOptions = PHVideoRequestOptions()
options.version = .original
options.isNetworkAccessAllowed = true
PHImageManager.default().requestAVAsset(forVideo: mPhasset, options: options, resultHandler: { (asset, audioMix, info) in
if let urlAsset = asset as? AVURLAsset {
let localVideoUrl = urlAsset.url
completionHandler(localVideoUrl)
} else {
completionHandler(nil)
}
})
}
}
编辑: None 重复的解决方案与我已有的有任何不同,它们都给出了 IMG_4227.JPG
类型名称而不是真实的姓名。所以这不是重复的。
我想通了。
let resources = PHAssetResource.assetResources(for: asset)
print("Filename: \((resources.first as! PHAssetResource).originalFilename)")
我正在为此寻找 Swift 或 Objective C 解决方案。在这里,我使用 swift 来展示我的例子。
我在 iOS 上使用照片框架,但很难找到真正的文件名而不是 IMG_4227.JPG
类型。我的图片名称实际上是 myawesomefilename.jpg
,如果我空投或将我的 iPhone 中的图片传输到保管箱,我会看到这个文件名。但是当我使用照片框架时,我得到的名称是 IMG_4227.JPG
.
app store上有一款app可以获取所有图片的真实文件名,绝对可以。我就是不知道怎么办。
这是我目前获取 IMG_4227.JPG
类型名称的方式:
func exifTapped(asset: PHAsset) {
print("name: \(asset.value(forKey: "filename"))")
getURL(ofPhotoWith: asset) { (url) in
print("URL: \(url)")
let data = NSData.init(contentsOf: url!)!
if let imageSource = CGImageSourceCreateWithData(data, nil) {
let imageProperties2 = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, nil)! as NSDictionary
print("imageProperties2: ", imageProperties2)
}
}
}
func getURL(ofPhotoWith mPhasset: PHAsset, completionHandler : @escaping ((_ responseURL : URL?) -> Void)) {
if mPhasset.mediaType == .image {
let options: PHContentEditingInputRequestOptions = PHContentEditingInputRequestOptions()
options.isNetworkAccessAllowed = true
options.canHandleAdjustmentData = {(adjustmeta: PHAdjustmentData) -> Bool in
return true
}
mPhasset.requestContentEditingInput(with: options, completionHandler: { (contentEditingInput, info) in
completionHandler(contentEditingInput!.fullSizeImageURL)
})
} else if mPhasset.mediaType == .video {
let options: PHVideoRequestOptions = PHVideoRequestOptions()
options.version = .original
options.isNetworkAccessAllowed = true
PHImageManager.default().requestAVAsset(forVideo: mPhasset, options: options, resultHandler: { (asset, audioMix, info) in
if let urlAsset = asset as? AVURLAsset {
let localVideoUrl = urlAsset.url
completionHandler(localVideoUrl)
} else {
completionHandler(nil)
}
})
}
}
编辑: None 重复的解决方案与我已有的有任何不同,它们都给出了 IMG_4227.JPG
类型名称而不是真实的姓名。所以这不是重复的。
我想通了。
let resources = PHAssetResource.assetResources(for: asset)
print("Filename: \((resources.first as! PHAssetResource).originalFilename)")