2013-07-19 30 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]; 
相關問題