CGPointZero 和 CGContextDrawImage 不可用

CGPointZero and CGContextDrawImage is unavailable

我无法将 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
}

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

Swift 2:

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

Swift 3:

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

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