【问题标题】:Swift : convert UIImageOrientation to ALAssetOrientationSwift:将 UIImageOrientation 转换为 ALAssetOrientation
【发布时间】:2014-08-04 18:13:45
【问题描述】:

我在使用 ENUMS 时遇到问题,特别是 UIImageOrientation 到 ALAssetOrientation

我试过了:

  var orientation : ALAssetOrientation = image.imageOrientation.toRaw()

  var orientation : ALAssetOrientation = image.imageOrientation as ALAssetOrientation

知道该怎么做吗?

【问题讨论】:

  • 我认为var orientation = ALAssetOrientation.fromRaw(image.imageOrientation.toRaw()) 应该可以工作。

标签: ios enums swift


【解决方案1】:

在swift中enumerationstype。它们不是objective cc枚举。枚举本身声明为struct,它们的关联值没有Int。所以不要认为swift和目标 c 枚举相同

使用以下代码将ALAssetOrientation 的枚举实例设为fromRaw

      var orientation : ALAssetOrientation = ALAssetOrientation.fromRaw(UIImage().imageOrientation.toRaw())!

由于fromRaw 在它不是有效的原始值的情况下返回可选,它返回 nil,因此在分配时您需要 unwrap 它或者您可以使用 var orientation : ALAssetOrientation! 隐式可选

请参考斯威夫特documentation

使用枚举的 fromRaw 方法尝试查找枚举 具有特定原始值的成员。并非所有可能的 Int 值都会 然而,找到一个匹配的行星。因此,fromRaw 方法 返回一个可选的枚举成员。

编辑在 Xcode 6.1 中使用

   var orientation : ALAssetOrientation = ALAssetOrientation(rawValue: UIImage().imageOrientation.rawValue)!

【讨论】:

  • 这个答案现在已经过时了。
  • @m00sey 对此感到抱歉。我已根据新 Xcode 编辑了答案。感谢您告诉我 :)
【解决方案2】:

fromRaw 已被 ALAssetOrientation 的初始化程序替换

ALAssetOrientation(rawValue: image.imageOrientation.rawValue)!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-31
    • 2014-10-06
    • 2017-10-10
    • 2015-10-24
    相关资源
    最近更新 更多