我只是写了一个小演示。它的所有功能都是使用 QLPreviewView 来快速查看 Pages 文件。应用运行时,可以滚动查看Pages文件内容,点击保存为PNG按钮,应用会将当前显示的内容保存为PNG图片文件。您可以在 save 方法中获得实现。我只是在该方法中尝试了两种实现,但都没有奏效。我刚刚得到一个空白图像,里面填充了窗口背景颜色。这里有一些建议吗?谢谢。
代码和应用程序屏幕截图可以在这里找到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