Objective-C/Cocoa to JXA - objective-c

I need to convert a PNG file to Base64 data so that I can add it to a JSON object, using JXA (JavaScript Application Scripting).
JXA is limited compared to regular JavaScript so I can't immediately use functions from FileReader, etc.
From what I've read, there is no way that I know how to do this without using Objective-C/Cocoa (which I only started reading about today for this task).
I found the following code in another post:
NSArray *keys = [NSArray arrayWithObject:#"NSImageCompressionFactor"];
NSArray *objects = [NSArray arrayWithObject:#"1.0"];
NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
NSImage *image = [[NSImage alloc] initWithContentsOfFile:[imageField stringValue]];
NSBitmapImageRep *imageRep = [[NSBitmapImageRep alloc] initWithData:[image TIFFRepresentation]];
NSData *tiff_data = [imageRep representationUsingType:NSPNGFileType properties:dictionary];
NSString *base64 = [tiff_data encodeBase64WithNewlines:NO];
I believe it is pertinent to what I am trying to do- does anybody know how I can bridge this method to use it in JXA?
I have been reading over the JXA Cookbook's section on Syntax for calling ObjC functions, but I am having difficulty understanding it... this is all that I have come up with so far:
var desktopString = app.pathTo("desktop").toString()
var file = `${desktopString}/test.png`
ObjC.import("Cocoa");
var image = $.NSImage.alloc.initWithContentsOfFile(file)
var imageRep = $.NSBitmapImageRep.alloc.initWithData(image)
But I don't know how to proceed- I am thrown off by:
The whole initial NSArray/NSDictionary part
TIFFRepresentation (Do I need it? Where do I put it?)
NSData *tiff_data = [imageRep representationUsingType:NSPNGFileType
properties:dictionary]; (There's no alloc! Why is dictionary needed?)
NSString *base64 = [tiff_data encodeBase64WithNewlines:NO]; (Again,
no alloc.)
I would be very appreciative if somebody could point me in the right direction / give me a few pointers on how I can accomplish what I am trying to do.
Thank you in advance!

Converting an image file into an NSImage representation and then onto a base-64 string is a lot of work, and was only applicable to the answer you sourced because the OP there was coming from a starting point of having NSImage class data. As you stated that you have a .png file, the route is much simpler:
ObjC.import('Foundation');
function fileToBase64(filepath) {
const standardizedPath = $.NSString.stringWithString(filepath)
.stringByStandardizingPath;
const base64String = $.NSData.dataWithContentsOfFile(standardizedPath)
.base64EncodedStringWithOptions(0);
return ObjC.unwrap(base64String);
}
(() => {
return fileToBase64('~/Desktop/test.png');
})();
For reference, this returns identical output to the following bash shell command:
base64 --input ~/Desktop/test.png
PS. For the benefit of learning, despite what the JXA Cookbook teaches, try not to import the entire Cocoa framework into your scripts, and just import the ones specific to the objective-c classes you're using.

Related

OS X: Comparing two images

I'm trying to implement a way to compare two images but I'm testing my and comparing the same image to make sure is working but doesn't work. here is may code:
NSImage *file = [[NSImage alloc] initWithContentsOfFile:path];
NSData *imgDataOne = [file TIFFRepresentation];
NSData *imgDataTwo = [file TIFFRepresentation];
if (imgDataOne == imgDataTwo)
{
NSLog(#"is the same image");
}
The if is never true. Any of you knows what I'm doing wrong or if another way to compare the images?
I'll really appreciate your help.
TIFFRepresentation will return a new NSData object. Comparing these objects using the == operator will always return false because these are two different objects.
NSData has isEqualToData method to test if these two NSData objects contain the same binary data.

Storing an array of CGLayer to file

I'm working on an OSX application where I need to store 446 CGLayers that get placed in a PDF context and am wondering if there's a way to write and read them from a file, rather than generating them when the application loads
I've read that CGLayer is no longer recommended, but I feel they really fit what I need. Also, if I use bitmapGraphicsContexts, they can pixelate when zooming in.
I am able to store them in NSArrays, both by storing them in NSValue and puting them into the array by bridging. I've also tried storing them in C arrays, but that didn't work out.
My problem comes when trying to store these arrays in a file. writeToFile: doesn't work with CGLayers, but NSKeyedArchiver/NSKeyedUnarchiver hasn't worked either, both when the layers are in NSValues or bridged.
Here's my method that attempts to write and read an array containing a single layer from a file.
+(CGLayerRef) colorAnnotations:(CGContextRef)context{
float symbolSize = 8;
CGRect glyphBox = CGRectMake(0,0, 8, 8);
CGLayerRef annotationLayer = CGLayerCreateWithContext (context,glyphBox.size, NULL);
CGContextRef annotaionLayerContext = CGLayerGetContext(annotationLayer);
CGMutablePathRef annot = CGPathCreateMutable();
//Drawing annotation
/*...*/
NSMutableArray *test = [[NSMutableArray alloc]init];
[test addObject:[[NSValue alloc] initWithBytes:&annotationLayer objCType:#encode(CGLayerRef)]];
//Getting file path in Documents directory
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:#"/annots.data"];
[NSKeyedArchiver archiveRootObject:test toFile:dataPath];
NSMutableArray *testLoads = [NSKeyedUnarchiver unarchiveObjectWithFile:dataPath];
CGLayerRef layerToReturn;
[[testLoads objectAtIndex:0]getValue:&layerToReturn];
return layerToReturn;
}
I get [NSKeyedArchiver encodeValueOfObjCType:at:]: unknown type encoding ('^')') was raisedfrom this, pretty sure because of the CGLayerRef type.
The lines needed to draw the different annotations I need are pretty long, so I've been trying to figure out a way to have them made and stored in a file without having to make them on startup each time. So far I'm not seeing a way to do this, but was hoping someone here may know of one and would appreciate any help.

OSX App Unrecognized Selector error when downloaded image from a when downloaded image from a JSON web service

In my Mac app I am getting Data from hey JSON web service and one of the fields is an image. When I do a NSLog(#"%#", [dict valueForKeyPath:#"profileImageDataString"] ); I see all of the data and it is correct. And I can also put it in a NSURL object like this NSURL *imgUrl = [dict valueForKeyPath:#"profileImageDataString"]; and it is still correct. But when I try this NSData *imgData = [NSData dataWithContentsOfURL:imgUrl]; or this NSImage *userImage = [[NSImage alloc] initWithContentsOfURL:imgUrl];I get the error unrecognized selector sent to instance. If I put the data in a NSString, two thirds is cut off at the end.
I am trying to save the Image data from the Jason web service into a text file to that later it can be loaded into a NSImage on in a user settings view.
Someone tell me what it is that I'm doing wrong?
This is the value of profileImageDataString that is returned by the web service;
iVBORw0KGgoAAAANSUhEUgAAATQAAAE0CAYAAACigc+fAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAACC6SURBVHhe7Z37t1VV+cb7W/SIgCYgl8FNDOIWEAevEJY3aKSgiYNogGCCCYRiIFIMLiomKKAChZ7KAjMC5ZKEIYQgcbPM5KqAtzHmt2f6XccNvOw9995zrTXfuZ4fPuM5A845a+35zv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0kcpOrQiH7QSahxaBGgQ6sDNCBVtxL9JCdkQVNxaEQ/6CBU3VpE6NCqAA1GjUOJfkqdWaJeHBqJB3QMqm4tMnRoDqChqHEo0Y/kzBKty6GReECHoOpWQodWFjQQVbeSeCjnzBKtyaER/aADUONQ8hV0aAJoGKpuJfHg4swSrcqhEf0g8NQ4lJwPHVoJaBCqbiX6cXFisn5g/g91hyU8vt0T+gAAAABJRU5ErkJggg==
The string seems to be a base64 encoded string representation of an image.
Try to create an NSData object from the string and the image from the data.
NSString *imgDataString = [dict objectForKey:#"profileImageDataString"];
NSData *imgData = [[NSData alloc] initWithBase64EncodedString:imgDataString options:0];
NSImage *userImage = [[NSImage alloc] initWithData:imgData];
And I can also put it in a NSURL object like this NSURL *imgUrl = [dict valueForKeyPath:#"profileImageDataString"]; and it is still correct.
You haven't got an NSURL object. You've got an NSString object. Declaring that your pointer refers to an NSURL and using the untyped getter valueForKeyPath: (or, even, much more correctly, objectForKey:) does not change the type of the object.
To convert a string to a URL use +[NSURL URLWithString:]. But what you're showing us isn't a URL. So why try to make it one at all?
But when I try this NSData *imgData = [NSData dataWithContentsOfURL:imgUrl]; or this NSImage *userImage = [[NSImage alloc] initWithContentsOfURL:imgUrl];I get the error unrecognized selector sent to instance.
That's because you're supplying an NSString, which does not implement the same selectors as an NSURL.
If I put the data in a NSString, two thirds is cut off at the end.
No it isn't; however when you log any object, all you get is enough information to help in debugging. Using NSLog is not intended to be a way to store the same amount of data in another form, or indeed to do anything strictly well defined other than help you to debug.
Get your string, use the approach as suggested by vadian.

PDF writing from iPad

I am able to generate a PDF with the following code. Can someone please help me with how I would add text to this? Thanks in advance.
-(void)createPDFfromUIView:(UIView*)aView saveToDocumentsWithFileName:(NSString*)aFilename
{
// Creates a mutable data object for updating with binary data, like a byte array
NSMutableData *pdfData = [NSMutableData data];
// Points the pdf converter to the mutable data object and to the UIView to be converted
UIGraphicsBeginPDFContextToData(pdfData, aView.bounds, nil);
UIGraphicsBeginPDFPage();
// draws rect to the view and thus this is captured by UIGraphicsBeginPDFContextToData
[aView drawRect:aView.bounds];
// remove PDF rendering context
UIGraphicsEndPDFContext();
// Retrieves the document directories from the iOS device
NSArray* documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
NSString* documentDirectory = [documentDirectories objectAtIndex:0];
NSString* documentDirectoryFilename = [documentDirectory stringByAppendingPathComponent:aFilename];
// instructs the mutable data object to write its context to a file on disk
[pdfData writeToFile:documentDirectoryFilename atomically:YES];
NSLog(#"documentDirectoryFileName: %#",documentDirectoryFilename);
}
I've never done this (yet, I plan getting on it tomorrow). But I think the right place to go is the Quartz 2D programming guide. It's also available from your developer documentation if you need it offline.
Basically you the same as you posted but appart from calling drawRect: you perform all the text drawing you want on that context.
There's useful information on writing simple strings on this guide
I hope it helps. It sure helped me!

How to make QTMovie play file from URL with forced (MP3) type?

I'm using QTKit to progressively download and play an MP3 from a URL. According to this documentation, this is the code I should use to accomplish that:
NSURL *mp3URL = [NSURL URLWithString:#"http://foo.com/bar.mp3"];
NSError *error = nil;
QTMovie *sound = [[QTMovie alloc] initWithURL:mp3URL error:&error];
[sound play];
This works, and does exactly what I want — the MP3 URL is lazily downloaded and starts playing immediately. However, if the URL does not have the ".mp3" path extension, it fails:
NSURL *mp3URL = [NSURL URLWithString:#"http://foo.com/bar"];
NSError *error = nil;
QTMovie *sound = [[QTMovie alloc] initWithURL:mp3URL error:&error];
[sound play];
No error is given, no exception is raised; the duration of the sound is just set to zero, and nothing plays.
The only way I have found to work around this is to force a type by loading the data manually and using a QTDataReference:
NSURL *mp3URL = [NSURL URLWithString:#"http://foo.com/bar"];
NSData *mp3Data = [NSData dataWithContentsOfURL:mp3URL];
QTDataReference *dataReference =
[QTDataReference dataReferenceWithReferenceToData:mp3Data
name:#"bar.mp3"
MIMEType:nil];
NSError *error = nil;
QTMovie *sound = [[QTMovie alloc] initWithDataReference:dataReference error:&error];
[sound play];
However, this forces me to completely download ALL of the MP3 synchronously before I can start playing it, which is obviously undesirable. Is there any way around this?
Thanks.
Edit
Actually, it seems that the path extension has nothing to do with it; the Content-Type is simply not being set in the HTTP header. Even so, the latter code works and the former does not. Anyone know of a way to fix this, without having access to the server?
Edit 2
Anyone? I can't find information about this anywhere, and Google frustratingly now shows this page as the top result for most of my queries...
Two ideas. (The first one being a bit hacky):
To work around the missing content type, you could embed a small Cocoa webserver that supplements the missing header field and route your NSURL over that "proxy".
Some Cocoa http server implementations:
http://code.google.com/p/cocoahttpserver/
http://cocoawithlove.com/2009/07/simple-extensible-http-server-in-cocoa.html
http://culturedcode.com/cocoa/
The second one would be, to switch to a lower level framework (From QTKit to AudioToolbox).
You'd need more code, but there are some very good resources out there on how to stream mp3 using AudioToolbox.
e.g.:
http://cocoawithlove.com/2008/09/streaming-and-playing-live-mp3-stream.html
Personally I'd go with the second option. AudioToolbox isn't as straightforward as QTKit but it offers a clean solution to your problem. It's also available on both - iOS and Mac OS - so you will find plenty of information.
Update:
Did you try to use another initializer? e.g.
+ (id)movieWithAttributes:(NSDictionary *)attributes error:(NSError **)errorPtr
You can insert your URL for the key QTMovieURLAttribute and maybe you can compensate the missing content type by providing other attributes in that dictionary.
This open source project has a QTMovie category that contains methods to accomplish similar things:
http://vidnik.googlecode.com/svn-history/r63/trunk/Source/Categories/QTMovie+Async.m
If you thought weichsel's first solution was hacky, you're going to love this one:
The culprit is the Content-Type header, as you have determined. Had QTKit.framework used Objective-C internally, this would be a trivial matter of overriding -[NSHTTPURLResponse allHeaderFields] with a category of your choosing. However, QTKit.framework (for better or worse) uses Core Foundation (and Core Services) internally. These are both C-based frameworks and there is no elegant way of overriding functions in C.
That said, there is a method, just not a pretty one. Function interposition is even documented by Apple, but seems to be a bit behind the times, compared to the remainder of their documentation.
In essence, you want something along the following lines:
typedef struct interpose_s {
void *new_func;
void *orig_func;
} interpose_t;
CFStringRef myCFHTTPMessageCopyHeaderFieldValue (
CFHTTPMessageRef message,
CFStringRef headerField
);
static const interpose_t interposers[] __attribute__ ((section("__DATA, __interpose"))) = {
{ (void *)myCFHTTPMessageCopyHeaderFieldValue, (void *)CFHTTPMessageCopyHeaderFieldValue }
};
CFStringRef myCFHTTPMessageCopyHeaderFieldValue (
CFHTTPMessageRef message,
CFStringRef headerField
) {
if (CFStringCompare(headerField, CFSTR("Content-Type"), 0) == kCFCompareEqualTo) {
return CFSTR("audio/x-mpeg");
} else {
return CFHTTPMessageCopyHeaderFieldValue(message, headerField);
}
}
You might want to add logic specific to your application in terms of handling the Content-Type field lest your application break in weird and wonderful ways when every HTTP request is determined to be an audio file.
Try replacing http:// with icy://.
Just create an instance like this...
QTMovie *aPlayer = [QTMovie movieWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
fileUrl, QTMovieURLAttribute,
[NSNumber numberWithBool:YES], QTMovieOpenForPlaybackAttribute,
/*[NSNumber numberWithBool:YES], QTMovieOpenAsyncOKAttribute,*/
nil] error:error];