You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Swift 将PNG转换为CGImage时CGContext始终返回nil问题求助

修复CGContext初始化失败方案

核心问题原因

你当前的代码触发了以下几个导致CGContext返回nil的问题:

  • 行字节数参数不匹配:你直接传入原始CGImage的bytesPerRow作为新Context的参数,但缩放后的图像宽度已发生变化,原始行字节数和新宽度不兼容,直接导致参数校验失败。
  • 大小写语法错误:代码中NSImage实例变量定义为nsImage,后续调用时写为小写的nsimage,Swift为大小写敏感语言,语法错误也会导致运行异常。
  • 可选值强制解包风险:多处使用!强制解包可选值,若图像加载失败会直接触发崩溃,同时部分图像的alphaInfo枚举值不符合CGContext的bitmapInfo要求,也会导致初始化失败。

修复代码

// 先安全校验图像加载结果
guard let inputPath = input as? String,
      let nsImage = NSImage(contentsOfFile: inputPath),
      let cgImage = nsImage.cgImage(forProposedRect: nil, context: nil, hints: nil) else {
    return nil
}

var ratio: Float = 0.0
let imageWidth = Float(cgImage.width)
let imageHeight = Float(cgImage.height)
let maxWidth = Float(nsImage.size.width)
let maxHeight = Float(nsImage.size.height)

if imageWidth > imageHeight {
    ratio = maxWidth / imageWidth
} else {
    ratio = maxHeight / imageHeight
}
if ratio > 1 {
    ratio = 1
}

let targetWidth = Int(imageWidth * ratio)
let targetHeight = Int(imageHeight * ratio)

guard let colorSpace = cgImage.colorSpace else { return nil }

// bytesPerRow传0让系统自动计算适配当前尺寸的行字节数,bitmapInfo补充alpha兼容处理
guard let cgContext = CGContext(
    data: nil,
    width: targetWidth,
    height: targetHeight,
    bitsPerComponent: cgImage.bitsPerComponent,
    bytesPerRow: 0, // 系统自动计算,避免参数不匹配
    space: colorSpace,
    bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue // 通用兼容的alpha配置,可根据你的图像需求调整
) else { return nil }

cgContext.interpolationQuality = .high
cgContext.draw(cgImage, in: CGRect(x: 0, y: 0, width: targetWidth, height: targetHeight))

guard let scaledCGImage = cgContext.makeImage() else { return nil }
return scaledCGImage

关键优化说明

  • bytesPerRow传0是Core Graphics的标准用法,系统会根据你传入的宽度、位深等参数自动计算最优的行字节数,不会出现参数不匹配的问题
  • 替换强制解包为可选绑定,避免图像加载失败时的崩溃问题
  • 使用通用兼容的alpha配置,适配绝大多数PNG、JPG图像的绘制需求
  • 修正变量大小写错误,避免语法异常

内容的提问来源于stack exchange,提问作者printDavid

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.26 11:24:07