gpt4 book ai didi

ios - swift 2.0 之后的 CGBitmapInfo alpha 掩码

转载 作者:IT王子 更新时间:2023-10-29 05:20:57 34 4
gpt4 key购买 nike

我正在使用这个 github 存储库中的一个库 https://github.com/Haneke/HanekeSwift缓存从服务器下载的图像。更新到 swift 2.0 后,一切都搞砸了。

除了这个功能,我已经能够修复所有问题:

func hnk_decompressedImage() -> UIImage! {
let originalImageRef = self.CGImage
let originalBitmapInfo = CGImageGetBitmapInfo(originalImageRef)
let alphaInfo = CGImageGetAlphaInfo(originalImageRef)

// See: http://stackoverflow.com/questions/23723564/which-cgimagealphainfo-should-we-use
var bitmapInfo = originalBitmapInfo
switch (alphaInfo) {
case .None:
bitmapInfo &= ~CGBitmapInfo.AlphaInfoMask
bitmapInfo |= CGBitmapInfo(rawValue: CGImageAlphaInfo.NoneSkipFirst.rawValue)
case .PremultipliedFirst, .PremultipliedLast, .NoneSkipFirst, .NoneSkipLast:
break
case .Only, .Last, .First: // Unsupported
return self
}

let colorSpace = CGColorSpaceCreateDeviceRGB()
let pixelSize = CGSizeMake(self.size.width * self.scale, self.size.height * self.scale)
if let context = CGBitmapContextCreate(nil, Int(ceil(pixelSize.width)), Int(ceil(pixelSize.height)), CGImageGetBitsPerComponent(originalImageRef), 0, colorSpace, bitmapInfo) {

let imageRect = CGRectMake(0, 0, pixelSize.width, pixelSize.height)
UIGraphicsPushContext(context)

// Flip coordinate system. See: http://stackoverflow.com/questions/506622/cgcontextdrawimage-draws-image-upside-down-when-passed-uiimage-cgimage
CGContextTranslateCTM(context, 0, pixelSize.height)
CGContextScaleCTM(context, 1.0, -1.0)

// UIImage and drawInRect takes into account image orientation, unlike CGContextDrawImage.
self.drawInRect(imageRect)
UIGraphicsPopContext()
let decompressedImageRef = CGBitmapContextCreateImage(context)

let scale = UIScreen.mainScreen().scale
let image = UIImage(CGImage: decompressedImageRef, scale:scale, orientation:UIImageOrientation.Up)

return image

} else {
return self
}
}

具体来说,这是抛出错误的代码行:

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

错误:一元运算符 '~' 不能应用于 CGBitmapInfo 类型的操作数

bitmapInfo |= CGBitmapInfo(rawValue: CGImageAlphaInfo.NoneSkipFirst.rawValue)

错误:二元运算符“|=”不能应用于“CGBitmapInfo”类型的操作数

if let context = CGBitmapContextCreate(nil, Int(ceil(pixelSize.width)), Int(ceil(pixelSize.height)), CGImageGetBitsPerComponent(originalImageRef), 0, colorSpace, bitmapInfo)

错误:无法将“CGBitmapInfo”类型的值转换为预期的 UInt32 类型参数

最佳答案

错误:无法将“CGBitmapInfo”类型的值转换为预期的 UInt32 类型参数

Swift 2.0 实际上需要一个 UInt32 而不是 CGBitMapInfo 对象,所以你应该在 CGBitMapInfo 中取出一个 UInt32 变量。

CGBitmapContextCreate(
nil,
Int(ceil(pixelSize.width)),
Int(ceil(pixelSize.height)),
CGImageGetBitsPerComponent(originalImageRef),
0,
colorSpace,
bitmapInfo.rawValue)

https://developer.apple.com/library/ios/documentation/GraphicsImaging/Reference/CGBitmapContext/#//apple_ref/c/func/CGBitmapContextCreate

关于ios - swift 2.0 之后的 CGBitmapInfo alpha 掩码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32639743/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com