initializing NSImage in swift - macos

Trying to initializing NSImage object.
var image = NSImage("Images/pause_work_normal.png")
But get an error: ambiguous reference to member NSImage.init

Try this:
var image:NSImage!
image = NSImage(named:"Images/pause_work_normal.png")
Or the short way
var image = NSImage(named:"Images/pause_work_normal.png")

Related

How do I change the pixelFormat in Metal?

If I try anyting other than bgra8Unorm, it will crash, saying,
-[MTLDebugRenderCommandEncoder validateFramebufferWithRenderPipelineState:]:1192: failed assertion `For color attachment 0, the render pipeline's pixelFormat (MTLPixelFormatBGRA8Unorm_sRGB) does not match the framebuffer's pixelFormat (MTLPixelFormatBGRA8Unorm).'
How do I change the framebuffer's pixelFormat then? I want to be able to do this:
PipelineDescriptor.colorAttachments[0].pixelFormat = .bgra8Unorm_srgb
Instead of this
PipelineDescriptor.colorAttachments[0].pixelFormat = .bgra8Unorm
Again, this is an attempt to fix this stupid bug.
You need the MetalView defined here:
var MetalView: MTKView {
return view as! MTKView
}
Then in viewDidLoad:
MetalView.colorPixelFormat = .bgra8Unorm_srgb
Then I can do this:
PipelineDescriptor.colorAttachments[0].pixelFormat = .bgra8Unorm_srgb
I figured it out myself yay!

Adding image inside textview using native NStextattachment and NSattributedString in Nativescript

I just tried to add an image inside the textview using native NSAtrributedstring and NStextAttachment, getting some help from this article here
However, I am unable to do it. I am using nativescript-mediafilepicker library to add the image from the Photo library, then converting the PH image to UIImage using one its inbuilt method. But the textview is not getting updated with the image. However, I am being able to add more string through the NSattributedstring but not image.
here's my code.
//creating and initilizing new NSMutableAttributedString
var attributedString = NSMutableAttributedString.alloc().initWithString(textview.ios.text);
textview.ios.attributedText = attributedString;
//value is an UIImage object what the convertPHImageToUIImage method returns
var image = value;
console.log(image);
//the above log prints<UIImage: 0x2817ac4d0> size {4032, 3024} orientation 0 scale 1.000000
let oldWidth = image.size.width;
// console.log(oldWidth);
let scaleFactor = oldWidth / (textview.ios.frame.size.width - 10);
//console.log(scaleFactor);
let orientation="up";
//create NStextAttachment
let textAttachment = NSTextAttachment.alloc().init();
//adding UIImage object to NSTextAttachment
textAttachment.image = UIImage.imageWithCGImageScaleOrientation(image.CGImage ,scaleFactor , orientation)
// console.dir(textAttachment);
//creating a new NSAttributedString
let attrStringWithImage = NSAttributedString.alloc().init();
//console.dir(attrStringWithImage);
attrStringWithImage.attachment = textAttachment;
console.dir(attrStringWithImage)
//appenind the NSAttributedString to the mutable string..
attributedString.appendAttributedString(attrStringWithImage);
//console.log(attributedString.containsAttachmentsInRange(textview.ios.selectedRange));
textview.ios.attributedText = attributedString;
//textview.ios.textStorage.insertAttributedStringAtIndex(attrStringWithImage,textview.ios.selectedRange.location)
//this doesn't work either
Install tns-platform-declarations if you are using TypeScript, that will make your life easy when you want to access native apis.
UIImage.imageWithCGImageScaleOrientation(cgImage, scale, orientation);
This docs will help you understanding the casting of Objective C to JavaScript / TypeScript.

How do you embed an image in an NSAttributedString?

I am trying to create an NSAttributedString that includes an NSImage for an OS X application.
I have tried a few different ways, but with this basic code:
let image = NSImage(named: "super-graphic")!
let attachment = NSTextAttachment()
attachment.image = image
let imageString = NSAttributedString(attachment: attachment)
When I set this on an NSLabel, or NSTextField attributed string, the image doesn't render.
Is it possible to combine NSImage and NSAttributedString to embed an image in an attributed string on OS X?
Well, I feel silly, this code works:
let image = NSImage(named: "super-graphic")!
let attachment = NSTextAttachment()
let cell = NSTextAttachmentCell(imageCell: image)
attachment.attachmentCell = cell
let imageString = NSAttributedString(attachment: attachment)
The key difference was to avoid using the "image" property of the NSTextAttachment

setResourceValue NSURLTagNamesKey error

I am getting an error when trying to set a tag color Blue to a file using setResourceValue:
var error: NSError?
let listofTags = NSWorkspace.sharedWorkspace().fileLabels
let theURL:NSURL = NSURL.fileURLWithPath("/Volumes/234567_fr.tif")!
var Tag: AnyObject = NSWorkspace.sharedWorkspace().fileLabels[4] // Tag = "Blue"
theURL.setResourceValue(Tag, forKey: NSURLTagNamesKey, error: &error)
println(error) // Error Domain=NSOSStatusErrorDomain Code=-8050 "The operation couldn’t be completed. (OSStatus error -8050.)
Any idea? Thank you for your help
Solution:
1 - The first argument for setResourceValue has to be an NSArray
2 - Shocking, but... the color name has to be the localized one!
This example fixes your error 8050 but doesn't actually set a color tag if your system language isn't English:
var error: NSError?
let theURL:NSURL = NSURL(fileURLWithPath: "/Users/me/tests/z.png")!
let tag: AnyObject = NSWorkspace.sharedWorkspace().fileLabels[4] // "Blue" tag
let arr = NSArray(object: tag)
theURL.setResourceValue(arr, forKey: NSURLTagNamesKey, error: &error)
On my system (French), this doesn't set an actual blue color label tag, only a text tag containing the word "Blue".
To set the proper color tag, you have to give the localized color name literally:
var error: NSError?
let theURL:NSURL = NSURL(fileURLWithPath: "/Users/me/tests/z.png")!
let arr = NSArray(object: "Bleu") // "Blue" translated to French
theURL.setResourceValue(arr, forKey: NSURLTagNamesKey, error: &error)

CIColorMap filter error

Trying to use CIColorMap filter, but getting run-time error "[NSImage _feImage]: unrecognized selector sent to instance 0x100163b10" when applying the filter
Following in the debugger, I see the RTE happens when the last line (return) of applyColorMap is executed. I know both of the (JPG) files exist (imageIn and the one initialized within the function). So ... any idea why I'm getting this error???
The doc for CIColorMap says: inputGradientImage = A CIImage class whose attribute type is CIAttributeTypeGradient and whose display name is Gradient Image. Is my issue because I have not identified the "attribute type"? How would I do that?
My code is like this ... in the main line:
CIImage *myResult = [self applyColorMap: inputCIimage];
Then the function being called is:
- (CIImage*) applyColorMap: (CIImage*)imageIn {
// Convert imageIn to B&W by using a gradient image half white / half black
NSString *gradientFP = [[NSString alloc] initWithString:[self myFilepath:
[NSString stringWithString:#"WB-1x20.jpg"]]];
NSImage *colormapImage = [[NSImage alloc] initWithContentsOfFile:gradientFP];
if (colormapImage == nil) {
NSLog (#"Bailing out. Gradient image allocation was NOT successful.");
return nil;
}
CIFilter *colorMapFilter = [CIFilter filterWithName:#"CIColorMap"];
//[colorMapFilter setDefaults];
[colorMapFilter setValue:imageIn forKey:#"inputImage"];
[colorMapFilter setValue:colormapImage forKey:#"inputGradientImage"];
return [colorMapFilter valueForKey:#"outputImage"]; //apply filter and return the new image
}
colormapImage needs to be a CIImage and not an NSImage, IIRC.

Resources