【问题标题】:CGPointZero and CGContextDrawImage is unavailableCGPointZero 和 CGContextDrawImage 不可用
【发布时间】:2016-10-18 10:25:02
【问题描述】:

我无法将 CGContextDrawImage 转换为 swift 3 ,我尝试了 context.draw 但它不起作用。有没有人可以给我反馈

Error screenshot

代码:

import UIKit

public struct Pixel {
public var value: UInt32

public var red: UInt8 {
    get {
        return UInt8(value & 0xFF)
    }
    set {
        value = UInt32(newValue) | (value & 0xFFFFFF00)
    }
}

public var green: UInt8 {
    get {
        return UInt8((value >> 8) & 0xFF)
    }
    set {
        value = (UInt32(newValue) << 8) | (value & 0xFFFF00FF)
    }
}

public var blue: UInt8 {
    get {
        return UInt8((value >> 16) & 0xFF)
    }
    set {
        value = (UInt32(newValue) << 16) | (value & 0xFF00FFFF)
    }
}

public var alpha: UInt8 {
    get {
        return UInt8((value >> 24) & 0xFF)
    }
    set {
        value = (UInt32(newValue) << 24) | (value & 0x00FFFFFF)
    }
}

public struct RGBAImage {
public var pixels: [Pixel]

public var width: Int
public var height: Int

public init?(image: UIImage) {
    guard let cgImage = image.cgImage else { return nil }

    // Redraw image for correct pixel format
    let colorSpace = CGColorSpaceCreateDeviceRGB()

    var bitmapInfo: UInt32 = CGBitmapInfo.byteOrder32Big.rawValue
    bitmapInfo |= CGImageAlphaInfo.premultipliedLast.rawValue & CGBitmapInfo.alphaInfoMask.rawValue

    width = Int(image.size.width)
    height = Int(image.size.height)
    let bytesPerRow = width * 4

    let imageData = UnsafeMutablePointer<Pixel>.allocate(capacity: width * height)

    guard let imageContext = CGContext(data: imageData, width: width, height: height, bitsPerComponent: 8, bytesPerRow: bytesPerRow, space: colorSpace, bitmapInfo: bitmapInfo) else { return nil }
    CGContextDrawImage(imageContext, CGRect(origin: CGPointZero, size: image.size), cgImage)

    let bufferPointer = UnsafeMutableBufferPointer<Pixel>(start: imageData, count: width * height)
    pixels = Array(bufferPointer)

    imageData.deinitialize()
    imageData.deallocate(capacity: width * height)
}

public func toUIImage() -> UIImage? {
    let colorSpace = CGColorSpaceCreateDeviceRGB()
    var bitmapInfo: UInt32 = CGBitmapInfo.byteOrder32Big.rawValue
    bitmapInfo |= CGImageAlphaInfo.premultipliedLast.rawValue & CGBitmapInfo.alphaInfoMask.rawValue

    let bytesPerRow = width * 4

    let imageDataReference = UnsafeMutablePointer<Pixel>(mutating: pixels)
    defer {
        imageDataReference.deinitialize()
    }
    let imageContext = CGContext(data: imageDataReference, width: width, height: height, bitsPerComponent: 8, bytesPerRow: bytesPerRow, space: colorSpace, bitmapInfo: bitmapInfo, releaseCallback: nil, releaseInfo: nil)

    guard let cgImage = imageContext!.makeImage() else {return nil}
    let image = UIImage(cgImage: cgImage)

    return image
}

【问题讨论】:

  • 您可能会在 SO 中找到一些,例如 thisthis。而且我在您的代码中找不到您尝试过的 context.draw
  • 要格式化您的代码,不要添加块引号,而是选择代码然后执行 CMD+K。我做了一次,然后你把它搞砸了,然后进行了编辑——请重新格式化。谢谢。
  • 在 Swift 3 中,它是 CGPoint.zerocontext.draw(...)
  • CGSizeZero 和 CGSizeMake 在 Swift 2.0 中可用,在 Swift 3.0 中,您可以与 CGSize 和 CGRect 一起使用。
  • @Alperen:你应该提供一个不工作的例子 context.draw 而不是 CGContextDrawImage 函数。

标签: ios swift3 cgcontextdrawimage


【解决方案1】:

所有 CoreGraphics C 函数都进行了重构,以更好地适应 Swift 风格:

斯威夫特 2:

CGContextDrawImage(imageContext, CGRect(origin: CGPointZero, size: image.size), cgImage)

斯威夫特 3:

imageContext.draw(cgImage, in: CGRect(origin: .zero, size: image.size))

附: .zero 可以用来代替 CGPoint.zero 因为它的类型是隐式推断的。

【讨论】:

    猜你喜欢
    • 2016-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-16
    • 1970-01-01
    相关资源
    最近更新 更多