gpt4 book ai didi

ios - NSImage/CGImage切片扩展错误: CGImageCreateWithImageProvider: invalid image size: 0 x 0

转载 作者:行者123 更新时间:2023-11-29 05:14:09 31 4
gpt4 key购买 nike

我在 NSImage 扩展中有一个方法 cgImageSlice,它应该返回包含每个图像切片的 [CGImage]。运行代码时,在第 18 行的第一个循环后,我收到错误 CGImageCreateWithImageProvider: invalid image size: 0 x 0 ,但矩形的大小并非如此。这是为什么?

extension NSImage {
var cgImage: CGImage? {
var proposedRect = CGRect(origin: .zero, size: size)
return cgImage(forProposedRect: &proposedRect, context: nil, hints: nil)
}

func sliceSize(axis: Axis, slices: Int) -> CGSize {
CGSize(width: axis == .horizontal ? size.width : size.width / CGFloat(slices), height: axis == .vertical ? size.height : size.height / CGFloat(slices))
}

func cgImageSlice(axis: Axis, slices: Int, sliceSize size: CGSize? = nil) -> [CGImage] {
let sliceSize = { () -> CGSize in
size == nil ? self.sliceSize(axis: axis, slices: slices) : size!
}()
var cgImages = [CGImage]()
for i in 0 ..< slices {
let rect = CGRect(x: CGFloat(i) * sliceSize.width, y: CGFloat(i) * sliceSize.height, width: sliceSize.width, height: sliceSize.height)
if let cgImage = cgImage?.cropping(to: rect) {
cgImages.append(cgImage)
}
}
return cgImages
}

func nsImageSlice(axis: Axis, slices: Int) -> [NSImage] {
let sliceSize = self.sliceSize(axis: axis, slices: slices)
var images = [NSImage]()
for cgImage in cgImageSlice(axis: axis, slices: slices, sliceSize: sliceSize) {
images.append(NSImage(cgImage: cgImage, size: sliceSize))
}
return images
}

enum Axis {
case horizontal
case vertical
}
}

最佳答案

我设法通过将矩形移到 for 循环之外来解决这个问题。我不太确定为什么会解决这个问题,但它确实解决了!

extension NSImage {
var cgImage: CGImage? {
var proposedRect = CGRect(origin: .zero, size: size)
return cgImage(forProposedRect: &proposedRect, context: nil, hints: nil)
}

func sliceSize(axis: Axis, slices: Int) -> CGSize {
CGSize(width: axis == .horizontal ? size.width : size.width / CGFloat(slices), height: axis == .vertical ? size.height : size.height / CGFloat(slices))
}

func cgImageSlice(axis: Axis, slices: Int, sliceSize size: CGSize? = nil) -> [CGImage] {
guard let cgImage = self.cgImage else {
return []
}
let sliceSize = { () -> CGSize in
size == nil ? self.sliceSize(axis: axis, slices: slices) : size!
}()
var cgImages = [CGImage]()
var rect = CGRect(origin: .zero, size: sliceSize)
for i in 1 ... slices {
if let cropped = cgImage.cropping(to: rect) {
cgImages.append(cropped)
}
rect.origin = CGPoint(x: 0, y: sliceSize.height * CGFloat(i))
}
return cgImages
}

func nsImageSlice(axis: Axis, slices: Int) -> [NSImage] {
let sliceSize = self.sliceSize(axis: axis, slices: slices)
var images = [NSImage]()
for cgImage in cgImageSlice(axis: axis, slices: slices, sliceSize: sliceSize) {
images.append(NSImage(cgImage: cgImage, size: sliceSize))
}
return images
}

enum Axis {
case horizontal
case vertical
}
}

关于ios - NSImage/CGImage切片扩展错误: CGImageCreateWithImageProvider: invalid image size: 0 x 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59355621/

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