【问题标题】:How to get the path of an image?如何获取图像的路径?
【发布时间】:2017-11-02 20:02:15
【问题描述】:

如何获取PHAsset 图片的路径或目录?

我遇到了@diegomen 编写的答案here,但是当我像selectedAssets[0].PHAsset.getURL() 一样尝试它时,它要求提供一个参数,但@diegomen 说它不需要来自 cmets 的参数。

我是不是用错了?

或者还有其他方法可以从PHAsset获取图片路径吗?

注意:我通过@Clay 找到了另一个解决方案here,但这似乎不是一个好的做法。

【问题讨论】:

  • 是的,您需要将完成块传递给该答案的 getURL 方法。阅读该答案下方的所有评论。
  • PHAsset 不是图像。它是包含许多有关图像和图像数据的信息的结构。您可以从中获得具有一定质量的 UIImage。

标签: ios swift phasset


【解决方案1】:

如果您已经在本地文件路径中存储了图像,并且您想从您的 PHAsset 图像地址中检索图像文件路径,请使用此函数:

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

资产=PHAsset *资产

【讨论】:

    【解决方案2】:

    您应该将图像存储到本地文件。之后,您将获得文件 url。

    NSData *imageData = UIImagePNGRepresentation(newImage);
    
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    
    NSString *imagePath =[documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png",@"cached"]];
    [imageData writeToFile:imagePath atomically:NO];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-08
      • 2012-12-01
      • 2014-09-27
      相关资源
      最近更新 更多