1

I just write a small demo. All of its feature is use the QLPreviewView to give a quick look of a Pages file. When the App runs, you can scroll to view the Pages file content, and when you click save to PNG button, the app will save the current content displayed into a PNG image file. You can get the implementation in the save method. I just tried two implementation in that method, neither of them worked. I just got a blank image filled with the window background color. Some advise here? Thanks.

The code and the App screen shot can be found here http://dr.ibuick.com/updU

#import <Cocoa/Cocoa.h>
#import <Quartz/Quartz.h>
#import <QuickLook/QuickLook.h>
#import "IBAppDelegate.h"

@interface IBAppDelegate (QLPreviewItem) <QLPreviewItem>

@end

@implementation IBAppDelegate (QLPreviewItem)

    - (NSURL *)previewItemURL
    {
        return self.resolvedFileURL;
    }

    - (NSString *)previewItemTitle
    {
        return [self.originalURL absoluteString];
    }

@end

@implementation IBAppDelegate

    - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
    {
        _resolvedFileURL = [NSURL fileURLWithPath:@"/Users/buick/Desktop/1.pages"];
        _originalURL = [NSURL fileURLWithPath:@"/Users/buick/Desktop/1.pages"];
        _previewView = [[QLPreviewView alloc] initWithFrame:NSMakeRect(0, 50, 480, 360) 
                        style:QLPreviewViewStyleNormal];
        [_previewView setPreviewItem:self];
        [self.window.contentView addSubview:_previewView];
    }

    - (IBAction)save:(id)sender {

        // Method 1

        [_previewView lockFocus];
        NSBitmapImageRep* rep = [_previewView     
            bitmapImageRepForCachingDisplayInRect:_previewView.bounds];
        [_previewView cacheDisplayInRect:_previewView.bounds toBitmapImageRep:rep];
        [_previewView unlockFocus];
        [[rep representationUsingType:NSPNGFileType properties:nil] 
            writeToFile:@"/Users/buick/Desktop/1.png" atomically:YES];

        // Method 2
        [_previewView lockFocus];

        NSBitmapImageRep *bits;
        bits = [[NSBitmapImageRep alloc]
            initWithFocusedViewRect:[_previewView visibleRect]];
        [_previewView unlockFocus];
        NSData *imageData;
        NSDictionary *imageProps = [NSDictionary dictionaryWithObject:[NSNumber             
            numberWithFloat:0.9] forKey:NSImageCompressionFactor];
        imageData = [bits representationUsingType:NSJPEGFileType     
            properties:imageProps];
        [imageData writeToFile:@"/Users/buick/Desktop/1.png" atomically:YES];
    }
@end

1 Answer 1

0

OK I think I got it. Try setting your QLPreviewView type to QLPreviewViewStyleCompact when you init it.

pv = [[QLPreviewView alloc] initWithFrame:pv.bounds
                                    style:QLPreviewViewStyleCompact];

Then use this category

NSImage+QuickLook from Matt Gemmel...

github

NSImage *image = [NSImage imageWithPreviewOfFileAtPath:[[self.files objectAtIndex:i] path] ofSize:NSMakeSize(1200, 1920) asIcon:NO];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.