本文介绍了与CGBitmapInfo和CGImageAlphaInfo位操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法与执行位操作 CGImageAlphaInfo CGBitmapInfo 斯威夫特。

I'm having trouble performing bitwise operations with CGImageAlphaInfo and CGBitmapInfo in Swift.

在特定的,我不知道如何该端口的Objective-C code:

In particular, I don't know how to port this Objective-C code:

bitmapInfo &= ~kCGBitmapAlphaInfoMask;
bitmapInfo |= kCGImageAlphaNoneSkipFirst;

下面简单雨燕端口产生略带神秘的编译器错误'CGBitmapInfo'不等同于'布尔'的最后一行:

bitmapInfo &= ~CGBitmapInfo.AlphaInfoMask
bitmapInfo |= CGImageAlphaInfo.NoneSkipFirst

查看源$ C ​​$ C,我注意到 CGBitmapInfo 声明为 RawOptionSetType ,而 CGImageAlphaInfo 不是。也许这有某种关系呢?

Looking at the source code I noticed that CGBitmapInfo is declared as a RawOptionSetType while CGImageAlphaInfo isn't. Maybe this has something to do with it?

这没有帮助,对位运算符官方文档不包括枚举。

It doesn't help that the official documentation on bitwise operators doesn't cover enums.

推荐答案

您有权利相当于雨燕code:

You have the right equivalent Swift code:

bitmapInfo &= ~CGBitmapInfo.AlphaInfoMask
bitmapInfo |= CGBitmapInfo(CGImageAlphaInfo.NoneSkipFirst.rawValue)

这是一个有点怪,因为 CGImageAlphaInfo 实际上不是一个位掩码 - 它只是一个UInt32的枚举(或一个CF_ENUM / NS_ENUM型 uint32_t的,在C的说法),通过7. 0值

It's a little strange because CGImageAlphaInfo isn't actually a bitmask -- it's just a UInt32 enum (or a CF_ENUM/NS_ENUM with type uint32_t, in C parlance), with values for 0 through 7.

什么是实际发生的事情是,你的第一行清除 BITMAPINFO 的前五位,其中的的一个位掩码(又名 RawOptionSetType 斯威夫特),因为 CGBitmapInfo.AlphaInfoMask 31,或0b11111。那么你的第二行粘 CGImageAlphaInfo 枚举的原始值到这些清除位。

What's actually happening is that your first line clears the first five bits of bitmapInfo, which is a bitmask (aka RawOptionSetType in Swift), since CGBitmapInfo.AlphaInfoMask is 31, or 0b11111. Then your second line sticks the raw value of the CGImageAlphaInfo enum into those cleared bits.

我还没有看到枚举和位掩码结合这样的其他地方,如果这解释了为什么没有真正的文档。由于 CGImageAlphaInfo 是一个枚举,其值是互斥的。这没有任何意义:

I haven't seen enums and bitmasks combined like this anywhere else, if that explains why there isn't really documentation. Since CGImageAlphaInfo is an enum, its values are mutually exclusive. This wouldn't make any sense:

bitmapInfo &= ~CGBitmapInfo.AlphaInfoMask
bitmapInfo |= CGBitmapInfo(CGImageAlphaInfo.NoneSkipFirst.rawValue)
bitmapInfo |= CGBitmapInfo(CGImageAlphaInfo.PremultipliedLast.rawValue)

这篇关于与CGBitmapInfo和CGImageAlphaInfo位操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-09 17:36