如何用EPSInside Rect将dataWithEPSInside Rect变成矢量而不是位图的矢量格式? [英] How to make dataWithEPSInsideRect vector rather than bitmap in vector format?

查看:0
本文介绍了如何用EPSInside Rect将dataWithEPSInside Rect变成矢量而不是位图的矢量格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在尝试将数据从NSImageView导出为EPS向量,但是导出似乎不是向量,而更像是EPS格式的位图。

有人能建议我哪里出错了吗?我怎样才能把它引向正确的方向?

s = [[graphicImage image] size];
offscreen = [[NSImageView alloc] initWithFrame:NSMakeRect(0,0,s.width*10, s.height)];
[offscreen setImage:[graphicImage image]];
export = [offscreen dataWithEPSInsideRect:[offscreen bounds]];

推荐答案

您正在使用NSImageView,它是用于显示栅格图像的视图。它返回栅格数据并不奇怪。

Quartz2D的首选元文件格式是PDF。有一些遗留的例程,很可能来自NextSTEP的Display PostScript时代,它专注于从视图和窗口生成EP。

您可以尝试创建一个屏幕外视图,设置该视图的DRAW方法以包括您的图形,然后使用该视图的dataWithEPSInsideRect:(NSRect)rect;方法。不过,我不知道这是否会产生一个矢量图形,或者只是一个视图内容的栅格图像。

我很好奇,在这个时代(后PDF时代),你正试图产生每股收益。我记得当我在Macromedia Free Hand工作的时候,EPS是特别有问题的。但祝你好运!

以下是使用NSView创建向量EPS文件的MacOS操场示例:

import Cocoa
import PlaygroundSupport

class  MyView : NSView {
    override func draw(_ rect: CGRect) {
        if let cgContext = NSGraphicsContext.current?.cgContext
        {
            cgContext.addRect(self.bounds.insetBy(dx: 20, dy: 20))
            cgContext.strokePath()
            cgContext.addEllipse(in: bounds.insetBy(dx: 40 , dy: 40))

            cgContext.setFillColor(NSColor.yellow.cgColor)
            cgContext.fillPath()

        }
    }
}

let myView = MyView(frame: CGRect(x: 0, y: 0, width: 320, height: 480))

PlaygroundSupport.PlaygroundPage.current.liveView = myView

let data = myView.dataWithEPS(inside: myView.bounds)
try data.write(to: URL.init(fileURLWithPath: "/Users/sthompson/Desktop/viewContent.eps"))

@interface MyView : NSView
@end

@implementation MyView

- (void) drawRect: (CGRect) rect
{
    CGContextRef cgContext = [[NSGraphicsContext currentContext] CGContext];

    CGContextSaveGState(cgContext);
    CGContextAddRect(cgContext, CGRectInset(self.bounds, 20, 20));
    CGContextStrokePath(cgContext);
    CGContextAddEllipseInRect(cgContext, CGRectInset(self.bounds, 40, 40));
    CGContextSetFillColorWithColor(cgContext, [[NSColor yellowColor] CGColor]);
    CGContextFillPath(cgContext);
    CGContextRestoreGState(cgContext);
}

+ (void) tryItOut {
    MyView *myView = [[MyView alloc] initWithFrame: NSRectFromCGRect(CGRectMake(0, 0, 320, 480))];

    NSData *data = [myView dataWithEPSInsideRect: myView.bounds];
    [data writeToURL: [NSURL fileURLWithPath: @"/Users/sthompson/Desktop/sampleEPSFile.eps"] atomically: YES];
}
@end

这篇关于如何用EPSInside Rect将dataWithEPSInside Rect变成矢量而不是位图的矢量格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆