Thursday, July 31, 2014

#1 2010-04-17 07:25:45 pm

SuperScripter
Member
Registered: 2010-03-09
Posts: 63

Creating an Image/PDF from contents of a view?

So I have a view in my window with a number of images that can either be turned on or off (hidden or not). Of course, which images are shown will change depending on what the user picks.

Is there a way I can put what is displayed in this view as a JPG? I'll settle for PDF, but JPG is much preferred. wink 50 bonus points for PNG! big_smile

My only thought is to "print" the view, and save it as a PDF somewhere. However, I don't want the user to see this happening. I want it to be a bit more professional than having them see a print dialog come and go. smile

-SuperScripter

Offline

 

#2 2010-04-18 01:43:21 am

StefanK
Member
From: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 10458
Website

Re: Creating an Image/PDF from contents of a view?

Hi,

AppleScript Studio can't do this directly.
It's possible in Objective-C by subclassing the view and add a method like

Code:

- (void)printViewToPNG
{

NSData *data;
NSBitmapImageRep *rep;
NSDictionary *properties;

rep = [self bitmapImageRepForCachingDisplayInRect:[self frame]];
[self cacheDisplayInRect:[self frame] toBitmapImageRep:rep];
data = [rep TIFFRepresentation];
properties = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO] forKey:NSImageInterlaced];
data = [rep representationUsingType:NSPNGFileType properties:properties];
[data writeToFile:[NSHomeDirectory() stringByAppendingPathComponent:@"Desktop/test.png"] atomically:YES];
}


regards

Stefan

Offline

 

#3 2010-04-18 04:30:14 am

Trash Man
Sanitation Department
Registered: 2005-10-20
Posts: 5336

Re: Creating an Image/PDF from contents of a view?

I have a question about the writeToFile:automatically: command. I know the function of the automattically part, but I'm wondering what's the best? YES or NO? Is it a difference in speed? Safety, maybe?

Thank you,
ief2


One mans trash is another mans treasure

Offline

 

#4 2010-04-18 05:03:31 am

StefanK
Member
From: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 10458
Website

Re: Creating an Image/PDF from contents of a view?

Only safety (YES), a human being is not able to notice the speed difference

Last edited by StefanK (2010-04-18 05:03:48 am)


regards

Stefan

Offline

 

#5 2010-04-18 05:54:20 am

StefanK
Member
From: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 10458
Website

Re: Creating an Image/PDF from contents of a view?

PS: Here a way without subclassing NSView.

Name the view in Interface Builder as "mainView" and connect it to awake From Nib
In Xcode create a new Objective-C class file (.m and .h), name it SKPrint

replace the contents of SKPrint.h with

Code:

#import <Cocoa/Cocoa.h>

@interface SKPrint : NSObject {}

+ (void)printView:(NSView *)view inPath:(NSString *)path mode:(NSString *)mode;

@end

replace the contents of SKPrint.m with

Code:

#import "SKPrint.h"

@implementation SKPrint

+ (void)printView:(NSView *)view inPath:(NSString *)path mode:(NSString *)mode
{
NSData *data;
NSBitmapImageRep *rep;
NSDictionary *properties;

if ([mode isEqual:@"pdf"]) {
data = [view dataWithPDFInsideRect:[view bounds]];
[data writeToFile:path atomically:YES];
}
else if ([mode isEqual:@"jpg"] || [mode isEqual:@"png"]) {
rep = [view bitmapImageRepForCachingDisplayInRect:[view frame]];
[view cacheDisplayInRect:[view frame] toBitmapImageRep:rep];
data = [rep TIFFRepresentation];
if ([mode isEqual:@"jpg"]) {
properties = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithFloat:0.9], NSImageCompressionFactor, [NSNumber numberWithBool:YES], NSImageProgressive, nil];
data = [rep representationUsingType:NSJPEGFileType properties:properties];
}
else {
properties = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO] forKey:NSImageInterlaced];
data = [rep representationUsingType:NSPNGFileType properties:properties];
}
[data writeToFile:path atomically:YES];
}
}

@end

in AppleScript add a property and some lines in the awake form nib handler

Applescript:


property mainView : null

on awake from nib theObject
   if name of theObject is "mainView" then
       set mainView to theObject
   end if
end awake from nib

then you can call the print method from AppleScript with

Applescript:


call method "printView:inPath:mode:" of class "SKPrint" with parameters {mainView, POSIXPath, "png"}

parameters:

mainView is the reference to the View
POSIX path is the full POSIX path to the file (quotation is not required)
"png" is the mode (possible values: "pdf", "jpg" and "png"


regards

Stefan

Offline

 

#6 2010-04-18 01:52:54 pm

SuperScripter
Member
Registered: 2010-03-09
Posts: 63

Re: Creating an Image/PDF from contents of a view?

Superb - thanks a bunch!

As a bit of a side note, what's a good resource for learning Cocoa? All of these commands seem so awesome, and I would love to be able to write full Cocoa Apps. I do know the basics, and some Obj-C, but I'm having a hard time finding a good resource for learning.

Any recommendations?

-SuperScripter

Offline

 

#7 2010-04-18 02:04:50 pm

StefanK
Member
From: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 10458
Website

Re: Creating an Image/PDF from contents of a view?

I studied with the book "Programming in Objective-C" by Stephen Kochan and the video tutorials at cocoacast.com


regards

Stefan

Offline

 

Board footer

Powered by FluxBB

[ Generated in 0.087 seconds, 8 queries executed ]

RSS (new topics) RSS (active topics)