gpt4 book ai didi

cocoa - 是否可以以编程方式在 Cocoa 中打印 IKImageBrowserView?

转载 作者:行者123 更新时间:2023-12-03 16:43:17 25 4
gpt4 key购买 nike

我想打印带有(内容)图像的 IKImageBrowserView。我尝试了以下代码

if (code == NSOKButton) {
NSPrintInfo *printInfo;
NSPrintInfo *sharedInfo;
NSPrintOperation *printOp;
NSMutableDictionary *printInfoDict;
NSMutableDictionary *sharedDict;

sharedInfo = [NSPrintInfo sharedPrintInfo];
sharedDict = [sharedInfo dictionary];
printInfoDict = [NSMutableDictionary dictionaryWithDictionary: sharedDict];

[printInfoDict setObject:NSPrintSaveJob
forKey:NSPrintJobDisposition];

[printInfoDict setObject:[sheet filename] forKey:NSPrintSavePath];

printInfo = [[NSPrintInfo alloc] initWithDictionary:printInfoDict];
[printInfo setHorizontalPagination: NSAutoPagination];
[printInfo setVerticalPagination: NSAutoPagination];
[printInfo setVerticallyCentered:NO];

printOp = [NSPrintOperation printOperationWithView:imageBrowser
printInfo:printInfo];

[printOp setShowsProgressPanel:NO];
[printOp runOperation];
}

因为 IKImageBrowserView 继承自 NSView 但打印预览显示空图像。请帮助我解决这个问题。提前致谢......

最佳答案

/*
1) allocate a c buffer at the size of the visible rect of the image
browser
*/
NSRect vRect = [imageBrowser visibleRect];
NSSize size = vRect.size;
NSLog(@"Size W = %f and H = %f", size.width, size.height);
void *buffer = malloc(size.width * size.height * 4);

//2) read the pixels using openGL
[imageBrowser lockFocus];
glReadPixels(0,
0,
size.width,
size.height,
GL_RGBA,
GL_UNSIGNED_BYTE,
buffer);
[imageBrowser unlockFocus];

//3) create a bitmap with those pixels
unsigned char *planes[2];
planes[0] = (unsigned char *) (buffer);

NSBitmapImageRep *imageRep = [[NSBitmapImageRep alloc]
initWithBitmapDataPlanes:planes pixelsWide:size.width
pixelsHigh:size.height bitsPerSample:8 samplesPerPixel:4 hasAlpha:YES
isPlanar:NO colorSpaceName:NSDeviceRGBColorSpace bitmapFormat:0
bytesPerRow:size.width*4 bitsPerPixel:32];
/*
4) create a temporary image with this bitmap and set it flipped
(because openGL and the AppKit don't have the same pixels coordinate
system)
*/
NSImage *img = [[NSImage alloc] initWithSize:size];
[img addRepresentation:imageRep];
[img setFlipped:YES];
[imageRep release];
/*
5) draw this temporary image into another image so that we get an
image without any reference to our "buffer" buffer so that we can
release it after that
*/
NSImage *finalImage = [[NSImage alloc] initWithSize:size];
[finalImage lockFocus];
[img drawAtPoint:NSZeroPoint
fromRect:NSMakeRect(0,0,size.width,size.height)
operation:NSCompositeCopy fraction:1.0];
[finalImage unlockFocus];

//[NSString stringWithFormat:@"/tmp/%@.tiff", marker]
NSData *imageData = [finalImage TIFFRepresentation];
NSString *writeToFileName = [NSString stringWithFormat:@"/Users/Desktop/%@.png", [NSDate date]];
[imageData writeToFile:writeToFileName atomically:NO];

//6) release intermediate objects
[img release];
free(buffer);

此后,我发送图像数据进行打印,这对我来说非常有用。

关于cocoa - 是否可以以编程方式在 Cocoa 中打印 IKImageBrowserView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10648544/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com