获取PHAsset的本地文件路径

时间:2015-02-04 17:39:35

标签: ios swift instagram phasset photosframework

我有一张照片,我希望我的用户能够在Instagram上分享,我需要获取照片的本地文件目录。

但是我将我的图像作为PHAsset获取(不是ALAsset所有其他答案似乎都涵盖了这个主题)。

查看PHAsset文档,我没有看到本地目录'变量

您知道如何使用新的ios8 Photo框架从用户相机胶卷获取照片的路径吗?

以下是我在用户照片卷中加载最后一张图片的代码

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)
    }

}

6 个答案:

答案 0 :(得分:9)

使用内容编辑输入:

asset.requestContentEditingInputWithOptions(PHContentEditingInputRequestOptions()) { (input, _) in
    let url = input.fullSizeImageURL
}

确保资产为PHAssetMediaType.Image,否则在解压缩可选项时可能会崩溃。

答案 1 :(得分:1)

func getImagesFromAsset(anAsset: PHAsset) {
    PHImageManager.default().requestImage(for: anAsset, targetSize: CGSize(width: 600, height: 600), contentMode: .aspectFill, options: nil, resultHandler: { (image, info) in
        //resultHandler have info ([AnyHashable: Any])
        print(info!["PHImageFileURLKey"])
        if let img = image {
            self.imgv.image = img
        }
    })
}

这可能会有所帮助 将此粘贴到CellForRowAt方法中,并在参数中传递PHAsset。

答案 2 :(得分:0)

是的,您必须将PHAsset转换为ALAsset,然后您可以从ALAsset中获取网址。 像这样的东西: PHImageManager.defaultManager()。requestAVAssetForVideo ....

答案 3 :(得分:0)

PHAsset *asset = YOUR_ASSET;

PHImageManager *manager = [PHImageManager defaultManager];
PHImageRequestOptions *options = [PHImageRequestOptions new];
options.synchronous = YES;
options.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat;
options.resizeMode = PHImageRequestOptionsResizeModeNone;
options.networkAccessAllowed = NO;

[manager requestImageForAsset:YOUR_ASSET targetSize:PHImageManagerMaximumSize contentMode:PHImageContentModeDefault options:options resultHandler:^(UIImage *resultImage, NSDictionary *info){

    NSURL *filePath = [info valueForKeyPath:@"PHImageFileURLKey"]

}];

答案 4 :(得分:0)

asset = PHAsset * asset

[asset requestContentEditingInputWithOptions:[PHContentEditingInputRequestOptions new] completionHandler:^(PHContentEditingInput *contentEditingInput, NSDictionary *info) {
   NSURL *imageURL = contentEditingInput.fullSizeImageURL;
}];

确保您的PHAsset应该是图像类型 检查此PHAssetMediaType.Image是否为真

答案 5 :(得分:-1)

请求开始内容编辑会话的资产信息。

- (PHContentEditingInputRequestID)requestContentEditingInputWithOptions:(PHContentEditingInputRequestOptions *)options 
                                                      completionHandler:(void (^)(PHContentEditingInput *contentEditingInput, NSDictionary *info))completionHandler;

包含完整尺寸图像数据的文件的URL。

@property(readonly, copy) NSURL *fullSizeImageURL;