Dropbox 2.0 Swift API:如何获取媒体元数据

时间:2016-04-28 21:08:34

标签: ios swift dropbox

我将SwiftyDropbox库用于iOS项目,递归获取文件夹列表,并检查文件是照片还是视频。

      client.files.getMetadata(path: fileURL, includeMediaInfo: true).response { response, error in

        if let file = response as? Files.FileMetadata {
          if file.mediaInfo != nil {

            // ??? how to get file.mediaInfo.metadata
            // specifically, I need the mediaMetadata

          }
        }
      }

我可以看到file.mediaInfo(如果它存在,则意味着存在元数据,但文档并未显示如何获取实际元数据本身(具体而言,照片的维度或视频的持续时间)。 / p>

我可以从file.mediaInfo的描述中获得这个(并解析从那里返回的字符串),但那是hacky而不是将来安全的。有没有其他方法来获取这些数据?

这是我想从(在Files.swift中)获取数据的类:

public class MediaMetadata: CustomStringConvertible {
    /// Dimension of the photo/video.
    public let dimensions : Files.Dimensions?
    /// The GPS coordinate of the photo/video.
    public let location : Files.GpsCoordinates?
    /// The timestamp when the photo/video is taken.
    public let timeTaken : NSDate?
    public init(dimensions: Files.Dimensions? = nil, location: Files.GpsCoordinates? = nil, timeTaken: NSDate? = nil) {
        self.dimensions = dimensions
        self.location = location
        self.timeTaken = timeTaken
    }
    public var description : String {
        return "\(prepareJSONForSerialization(MediaMetadataSerializer().serialize(self)))"
    }
}

1 个答案:

答案 0 :(得分:4)

以下是一个示例:

Dropbox.authorizedClient!.files.getMetadata(path: "/test.jpg", includeMediaInfo: true).response { response, error in
    if let result = response as? Files.FileMetadata {
        print(result.name)

        if result.mediaInfo != nil {
            switch result.mediaInfo! as Files.MediaInfo {
            case .Pending:
                print("Media info is pending...")
            case .Metadata(let mediaMetadata):
                print(mediaMetadata.dimensions)
                print(mediaMetadata.location)
                print(mediaMetadata.timeTaken)
            }
        }
    } else {
        print(error!)
    }
}