Swift:将UIImageOrientation转换为ALAssetOrientation

wee*_*Pee 1 enums ios swift

我在使用ENUMS时遇到了问题,尤其是对于ALAssetOrientation的UIImageOrientation

我试过了 :

  var orientation : ALAssetOrientation = image.imageOrientation.toRaw()
Run Code Online (Sandbox Code Playgroud)

  var orientation : ALAssetOrientation = image.imageOrientation as ALAssetOrientation
Run Code Online (Sandbox Code Playgroud)

知道应该怎么做吗?

cod*_*ter 5

在swift enumerationsare type.他们不是objective cc枚举.Enums本身被宣称为struct和他们没有相关的价值Int.所以不要认为快速和客观的c enums相同

使用下面的代码来创建ALAssetOrientationwith 的枚举实例fromRaw

      var orientation : ALAssetOrientation = ALAssetOrientation.fromRaw(UIImage().imageOrientation.toRaw())!
Run Code Online (Sandbox Code Playgroud)

因为fromRaw返回是可选的,如果它是无效的原始值,它返回nil,所以在分配时你需要unwrap它,或者你可以使用var orientation : ALAssetOrientation!隐式可选

请参阅swift 文档

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

编辑在Xcode 6.1中使用

   var orientation : ALAssetOrientation = ALAssetOrientation(rawValue: UIImage().imageOrientation.rawValue)!
Run Code Online (Sandbox Code Playgroud)