获取PHAsset的本地文件路径 [英] Get the local file path of a PHAsset
问题描述
我有一张照片,我希望我的用户能够在Instagram上分享,我需要获取照片的本地文件目录。
I have a photo I want my user to be able to share on Instagram and I need to get the local file directory of the photo.
但是我正在抓取我的图像作为PHAsset(不是ALAsset所有其他答案似乎涵盖了这个主题)。
However I am fetching my images as a PHAsset (not the ALAsset all the other answers seem to cover on this subject).
查看PHAsset文档我没有看到'本地目录'变量。
Looking at the PHAsset documentation I don't see a 'local directory' variable.
你知道如何使用新的ios8 Photo框架从用户相机胶卷获取照片的路径吗?
Do you know how I can get the path to a photo from the users camera roll using the new ios8 Photo's framework?
这是我在用户照片卷中加载最后一张图片的代码
Here is my code for loading the last image in the users photo roll
public func loadLastPhotoIntoGalleryIcon()
{
if(PHPhotoLibrary.authorizationStatus() != PHAuthorizationStatus.Authorized)
{
return
}
var fetchOptions:PHFetchOptions = PHFetchOptions()
fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
var fetchResult:PHFetchResult = PHAsset.fetchAssetsWithMediaType(PHAssetMediaType.Image, options: fetchOptions)
var lastAsset:PHAsset = fetchResult.firstObject as PHAsset
var options:PHImageRequestOptions = PHImageRequestOptions()
options.version = PHImageRequestOptionsVersion.Current
PHImageManager.defaultManager().requestImageForAsset(lastAsset, targetSize: _view.getGalleryIconSize(), contentMode: PHImageContentMode.AspectFill, options: options)
{
(result, objects) -> Void in
self._view.setGalleryIcon(result)
}
}
推荐答案
使用内容编辑输入:
asset.requestContentEditingInputWithOptions(PHContentEditingInputRequestOptions()) { (input, _) in
let url = input.fullSizeImageURL
}
确保资产是 PHAssetMediaType.Image
,否则在解压缩可选项时可能会崩溃。
Make sure that the asset is PHAssetMediaType.Image
or otherwise it may crash when unpacking an optional.
这篇关于获取PHAsset的本地文件路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!