2013-07-19 29 views
0

我只写了一个小型演示。它的所有功能都是使用QLPreviewView来快速查看页面文件。 当应用程序运行时,您可以滚动查看Pages文件内容,并且当您单击保存到PNG按钮时,应用程序会将当前显示的内容保存到PNG图像文件中。你可以在保存方法中获得实现。我只是尝试了两种方法的实现,他们都没有工作。 我刚刚得到一个填充了窗口背景色的空白图像。有人建议在这里?谢谢。如何获取QLPreviewView内容并写入图像

的代码和应用程​​序的屏幕截图可以在这里找到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 

回答

0

OK,我想我得到了它。尝试在初始化时将QLPreviewView类型设置为QLPreviewViewStyleCompact。

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

然后使用这一类NSImage+QuickLook from Matt Gemmel...

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