作者热门文章
- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我正在使用这个 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)
关于ios - swift 2.0 之后的 CGBitmapInfo alpha 掩码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32639743/
我是一名优秀的程序员,十分优秀!