NSImageView (added programmatically) doesn't show image, but shows color












0














Swift 4. Very simple project, all I did - just added a NSImageView programmatically, backgroundColor and NSImage from the .jpg file. I see the good pink color, but can't see the image at all! I tried many different approaches and some was successful (Image showed up well in collection view and if NSImageView was added manually in the story board) but I need in simple programmatically method. Here is all of my code:



class ViewController: NSViewController {
var image: NSImage = NSImage()
var ivTest = NSImageView()

override func viewDidLoad() {
super.viewDidLoad()
self.view.addSubview(self.ivTest)
self.ivTest.wantsLayer = true
self.ivTest.layer?.backgroundColor = NSColor.systemPink.cgColor
self.ivTest.layer?.frame = NSRect(x: 0, y: 0, width: 100, height: 100)

let manager = FileManager.default
var url = manager.urls(for: .documentDirectory, in: .userDomainMask).first
url = url?.appendingPathComponent("night.jpg")
image = NSImage(byReferencing: url!)
if (image.isValid == true){
print("valid")
print("image size (image.size.width):(image.size.height)")
self.ivTest.image = image
} else {
print("not valid")
}
}

override var representedObject: Any? {
didSet {
// Update the view, if already loaded.
}
}
}


output:
enter image description here



result:
enter image description here



thank so much...



--- edited ---



Yes, thank You! Just added this and saw image:



self.ivTest.frame = NSRect(x: 0, y: 0, width: 100, height: 100)









share|improve this question




















  • 2




    What is the frame of self.ivTest?
    – Willeke
    Nov 21 at 5:12






  • 1




    As Willeke mentioned, your frame is not configured in your code. set frame for ivTest or use autolayout to give ivTest size.
    – brianLikeApple
    Nov 21 at 9:58










  • Yes, I forgot that layer.frame and frame are different )) , thanks!
    – Valikoo Vakh
    Nov 21 at 15:16
















0














Swift 4. Very simple project, all I did - just added a NSImageView programmatically, backgroundColor and NSImage from the .jpg file. I see the good pink color, but can't see the image at all! I tried many different approaches and some was successful (Image showed up well in collection view and if NSImageView was added manually in the story board) but I need in simple programmatically method. Here is all of my code:



class ViewController: NSViewController {
var image: NSImage = NSImage()
var ivTest = NSImageView()

override func viewDidLoad() {
super.viewDidLoad()
self.view.addSubview(self.ivTest)
self.ivTest.wantsLayer = true
self.ivTest.layer?.backgroundColor = NSColor.systemPink.cgColor
self.ivTest.layer?.frame = NSRect(x: 0, y: 0, width: 100, height: 100)

let manager = FileManager.default
var url = manager.urls(for: .documentDirectory, in: .userDomainMask).first
url = url?.appendingPathComponent("night.jpg")
image = NSImage(byReferencing: url!)
if (image.isValid == true){
print("valid")
print("image size (image.size.width):(image.size.height)")
self.ivTest.image = image
} else {
print("not valid")
}
}

override var representedObject: Any? {
didSet {
// Update the view, if already loaded.
}
}
}


output:
enter image description here



result:
enter image description here



thank so much...



--- edited ---



Yes, thank You! Just added this and saw image:



self.ivTest.frame = NSRect(x: 0, y: 0, width: 100, height: 100)









share|improve this question




















  • 2




    What is the frame of self.ivTest?
    – Willeke
    Nov 21 at 5:12






  • 1




    As Willeke mentioned, your frame is not configured in your code. set frame for ivTest or use autolayout to give ivTest size.
    – brianLikeApple
    Nov 21 at 9:58










  • Yes, I forgot that layer.frame and frame are different )) , thanks!
    – Valikoo Vakh
    Nov 21 at 15:16














0












0








0







Swift 4. Very simple project, all I did - just added a NSImageView programmatically, backgroundColor and NSImage from the .jpg file. I see the good pink color, but can't see the image at all! I tried many different approaches and some was successful (Image showed up well in collection view and if NSImageView was added manually in the story board) but I need in simple programmatically method. Here is all of my code:



class ViewController: NSViewController {
var image: NSImage = NSImage()
var ivTest = NSImageView()

override func viewDidLoad() {
super.viewDidLoad()
self.view.addSubview(self.ivTest)
self.ivTest.wantsLayer = true
self.ivTest.layer?.backgroundColor = NSColor.systemPink.cgColor
self.ivTest.layer?.frame = NSRect(x: 0, y: 0, width: 100, height: 100)

let manager = FileManager.default
var url = manager.urls(for: .documentDirectory, in: .userDomainMask).first
url = url?.appendingPathComponent("night.jpg")
image = NSImage(byReferencing: url!)
if (image.isValid == true){
print("valid")
print("image size (image.size.width):(image.size.height)")
self.ivTest.image = image
} else {
print("not valid")
}
}

override var representedObject: Any? {
didSet {
// Update the view, if already loaded.
}
}
}


output:
enter image description here



result:
enter image description here



thank so much...



--- edited ---



Yes, thank You! Just added this and saw image:



self.ivTest.frame = NSRect(x: 0, y: 0, width: 100, height: 100)









share|improve this question















Swift 4. Very simple project, all I did - just added a NSImageView programmatically, backgroundColor and NSImage from the .jpg file. I see the good pink color, but can't see the image at all! I tried many different approaches and some was successful (Image showed up well in collection view and if NSImageView was added manually in the story board) but I need in simple programmatically method. Here is all of my code:



class ViewController: NSViewController {
var image: NSImage = NSImage()
var ivTest = NSImageView()

override func viewDidLoad() {
super.viewDidLoad()
self.view.addSubview(self.ivTest)
self.ivTest.wantsLayer = true
self.ivTest.layer?.backgroundColor = NSColor.systemPink.cgColor
self.ivTest.layer?.frame = NSRect(x: 0, y: 0, width: 100, height: 100)

let manager = FileManager.default
var url = manager.urls(for: .documentDirectory, in: .userDomainMask).first
url = url?.appendingPathComponent("night.jpg")
image = NSImage(byReferencing: url!)
if (image.isValid == true){
print("valid")
print("image size (image.size.width):(image.size.height)")
self.ivTest.image = image
} else {
print("not valid")
}
}

override var representedObject: Any? {
didSet {
// Update the view, if already loaded.
}
}
}


output:
enter image description here



result:
enter image description here



thank so much...



--- edited ---



Yes, thank You! Just added this and saw image:



self.ivTest.frame = NSRect(x: 0, y: 0, width: 100, height: 100)






cocoa swift4






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 at 15:18

























asked Nov 21 at 1:22









Valikoo Vakh

344




344








  • 2




    What is the frame of self.ivTest?
    – Willeke
    Nov 21 at 5:12






  • 1




    As Willeke mentioned, your frame is not configured in your code. set frame for ivTest or use autolayout to give ivTest size.
    – brianLikeApple
    Nov 21 at 9:58










  • Yes, I forgot that layer.frame and frame are different )) , thanks!
    – Valikoo Vakh
    Nov 21 at 15:16














  • 2




    What is the frame of self.ivTest?
    – Willeke
    Nov 21 at 5:12






  • 1




    As Willeke mentioned, your frame is not configured in your code. set frame for ivTest or use autolayout to give ivTest size.
    – brianLikeApple
    Nov 21 at 9:58










  • Yes, I forgot that layer.frame and frame are different )) , thanks!
    – Valikoo Vakh
    Nov 21 at 15:16








2




2




What is the frame of self.ivTest?
– Willeke
Nov 21 at 5:12




What is the frame of self.ivTest?
– Willeke
Nov 21 at 5:12




1




1




As Willeke mentioned, your frame is not configured in your code. set frame for ivTest or use autolayout to give ivTest size.
– brianLikeApple
Nov 21 at 9:58




As Willeke mentioned, your frame is not configured in your code. set frame for ivTest or use autolayout to give ivTest size.
– brianLikeApple
Nov 21 at 9:58












Yes, I forgot that layer.frame and frame are different )) , thanks!
– Valikoo Vakh
Nov 21 at 15:16




Yes, I forgot that layer.frame and frame are different )) , thanks!
– Valikoo Vakh
Nov 21 at 15:16

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53404006%2fnsimageview-added-programmatically-doesnt-show-image-but-shows-color%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53404006%2fnsimageview-added-programmatically-doesnt-show-image-but-shows-color%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

TypeError: fit_transform() missing 1 required positional argument: 'X'