Swift OSX - 在IKImageView上设置NSImage

时间:2017-09-06 10:57:20

标签: swift macos

我正在尝试在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)
    }
}

1 个答案:

答案 0 :(得分:1)

这样的事情可以解决问题:

CGImageSourceRef source;

source = CGImageSourceCreateWithData((CFDataRef)[cgImagem TIFFRepresentation], NULL);

NSDictionary* props = (NSDictionary*)CGImageSourceCopyPropertiesAtIndex(source, 0, NULL);

imagemIKImageView.setImage(cgImagem, imageProperties: props)