您好,我正在嘗試將資料從 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]];
uj5u.com熱心網友回復:
您使用的NSImageView是用于顯示光柵影像的視圖。它回傳柵格資料應該不足為奇。
Quartz2D 首選的元檔案格式是 PDF。周圍有一些遺留例程,可能來自 NextSTEP 的顯示后記日,專注于從視圖和視窗生成 EPS。
您可以嘗試創建一個螢屏外視圖,設定視圖的繪制方法以包含您的圖形,然后使用視圖的dataWithEPSInsideRect:(NSRect)rect;方法。我不知道這是否會產生矢量圖,或者只是視圖內容的光柵影像。
我很好奇你在這個時代(后 PDF 時代)試圖生成 EPS。我記得在我為 Macromedia FreeHand 作業的日子里,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
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/342724.html
標籤:目标-c 苹果系统 数据 nsimageview nsrect
