Nick Balistreri Nick Balistreri - 2 months ago 12
Objective-C Question

What is the equivalent of UIImage imageWithData for NSImage

I want to load an image into my MAC OS desktop application. I have found plenty of documentation on how to do this on mobile iOS but how do I load the image on my desktop app since there is no UIImage(imageWithData).

NSData * imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString:@"myurl"]];
productImage.image = [NSImage imageWithData:imageData];

Answer

imageWithData: is a convenience initializer for initWithData: in UIKIt. AppKit doesn't seem to have this, so you'll just need to use initWithData: like this:

productImage.image = [[NSImage alloc] initWithData:imageData];

See this page for more info: https://developer.apple.com/reference/appkit/nsimage/1519941-initwithdata?language=objc