NSImage PDF -> JPG

Sage
Posts: 1,403
Joined: 2005.07
Post: #1
Does anyone know how to convert an NSImage loaded by imageNamed@"somthing.pdf", the type you get with screenshots on .3.9, into a jpeg?

Sir, e^iπ + 1 = 0, hence God exists; reply!
Quote this message in a reply
Moderator
Posts: 1,140
Joined: 2005.07
Post: #2
You can open it with NSImage's open image with file init method, then create an NSBitmapImageRep from the TIFFRepresentation of the image. After that, you can get the NSData for various image formats and save them to the disk. (look at the documentation for the exact method names)

Edit: basically exactly what you did with your GlyphTool, but first load the image using NSImage's initWithContentsOfFile method.
Quote this message in a reply
Sage
Posts: 1,403
Joined: 2005.07
Post: #3
Oh that works!
Thanks so much I was getting really stressed about not being able to convert pdf's.

Code if anyone wants to convert whatever image type into a jpg
Code:
#import <Cocoa/Cocoa.h>

int main (int argc, const char * argv[]) {
    if(argc != 3)
        return 0;
    
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    
    NSApplicationLoad();
    
    NSImage *image = [[NSImage alloc] initWithContentsOfFile:[NSString stringWithCString:argv[1]]];
    NSData *tiffRep = [image TIFFRepresentation];
    
    NSBitmapImageRep *rep = [[NSBitmapImageRep alloc] initWithData:tiffRep];
    
    NSData *bitmapData = [rep representationUsingType:NSJPEGFileType properties:NULL];
    [bitmapData writeToFile:[NSString stringWithCString:argv[2]] atomically:YES];
    
    [pool release];
    return 0;
}

Sir, e^iπ + 1 = 0, hence God exists; reply!
Quote this message in a reply
Moderator
Posts: 771
Joined: 2003.04
Post: #4
[hint]Bits of code like this would would make an interesting Wiki[/hint]
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Basic NSImage drawing help please! xenocide 6 5,680 Feb 2, 2009 11:01 AM
Last Post: FlamingHairball
  NSImage and PNG's bmantzey 3 5,079 Nov 29, 2008 07:01 PM
Last Post: arekkusu
  How to get resolution of an NSImage or NSBitmapImageRep? aegidian 4 5,705 Oct 18, 2005 02:39 AM
Last Post: aegidian
  Help with NSImage and NSString... Joseph Duchesne 2 4,871 Sep 30, 2005 02:21 PM
Last Post: unknown
  NSImage -&gt; Jigsaw peices unknown 5 3,713 Sep 29, 2005 07:00 AM
Last Post: unknown