我试图在IKImageView上设置NSImage的图像。我已经使用NSImage.CGImage从NSImage获取了CGImage,但是要在IKImageView上设置它,我需要图像的属性。我怎样才能得到这些财产?
import Cocoa
import Quartz
class AjustarImagemViewController: NSViewController {
@IBOutlet weak var imagemIKImageView: IKImageView!
var imagem: NSImage? //Receive an image from another view
override func viewDidLoad() {
super.viewDidLoad()
print(imagem)
if let imagemRecebida = imagem {
let cgImagem = imagemRecebida.cgImage(forProposedRect: nil, context: nil, hints: nil)
imagemIKImageView.setImage(cgImagem, imageProperties: [AnyHashable : Any])
}
}
@IBAction func voltarButtonClicked(_ sender: NSButton) {
dismiss(self)
}
}
最佳答案
像这样的事情应该能起到作用:
CGImageSourceRef source;
source = CGImageSourceCreateWithData((CFDataRef)[cgImagem TIFFRepresentation], NULL);
NSDictionary* props = (NSDictionary*)CGImageSourceCopyPropertiesAtIndex(source, 0, NULL);
imagemIKImageView.setImage(cgImagem, imageProperties: props)
关于swift - Swift OSX-在IKImageView上设置NSImage,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46073419/