gpt4 book ai didi

cocoa - swift NSImage 到 CGImage

转载 作者:IT王子 更新时间:2023-10-29 05:25:01 28 4
gpt4 key购买 nike

如何在 Swift 中将 NSImage 转换为 CGImage?在 Objective-C 中我是这样做的:

- (CGImageRef)CGImage {
NSData *imageData = self.TIFFRepresentation;
CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)imageData, NULL);
CGImageRef maskRef = CGImageSourceCreateImageAtIndex(source, 0, NULL);
return maskRef;
}

现在我尝试了:

extension NSImage {
var CGImage: CGImageRef {
get {
let imageData = self.TIFFRepresentation
let source = CGImageSourceCreateWithData(imageData as CFDataRef, nil)
let maskRef = CGImageSourceCreateImageAtIndex(source, UInt(0), nil)
return maskRef;
}
}
}

我无法编译,出现错误:无法在 let maskRef ...< 行找到接受提供的参数的“init”的重载/

最佳答案

这是我用来将 NSImage 转换为 CGImage 的内容:

let image = NSImage(named:"image")
if let image = image {
var imageRect = CGRect(x: 0, y: 0, width: image.size.width, height: image.size.height)
let imageRef = image.cgImage(forProposedRect: &imageRect, context: nil, hints: nil)
}

关于cocoa - swift NSImage 到 CGImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24595908/

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