gpt4 book ai didi

ios - CGPointZero 和 CGContextDrawImage 不可用

转载 作者:可可西里 更新时间:2023-11-01 05:11:13 26 4
gpt4 key购买 nike

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

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

关于ios - CGPointZero 和 CGContextDrawImage 不可用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40105725/

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