gpt4 book ai didi

objective-c - 在 cocoa osx 中将位图图像复制到粘贴板时遇到问题

转载 作者:行者123 更新时间:2023-12-03 16:41:20 26 4
gpt4 key购买 nike

我正在编写一个计算位图图像的图像处理应用程序。它在多个窗口中显示它们,我希望能够将图像复制到粘贴板。每个窗口都有自己的 Controller (NSWindowController 的子类)和 imageView(NSImageView 的子类)。

我的代码将成功从最近显示的窗口复制图像,但当我从以前显示的窗口复制时,会给出 EXC_BAD_ACCESS(代码 1)。

窗口 Controller 中用于设置位图图像的代码是这样的:

-(void) placeImage:(NSRect) theRect{
windowRect = theRect;
[[self window] setTitle:windowName];

NSBitmapImageRep* bitmap = [[NSBitmapImageRep alloc]
initWithBitmapDataPlanes: iBitmap.getpixdatap()
pixelsWide: iBitmap.getwidth() pixelsHigh: iBitmap.getheight()
bitsPerSample: 8 samplesPerPixel: 4 hasAlpha: YES isPlanar:NO
colorSpaceName:NSCalibratedRGBColorSpace
bytesPerRow: 4*iBitmap.getwidth()
bitsPerPixel: 32];

NSImage* im = [[NSImage alloc] initWithSize:NSMakeSize(iBitmap.getwidth(), iBitmap.getheight())];
[im addRepresentation:bitmap];

//NSImage* im = [[NSImage alloc] initByReferencingFile:@"./Contents/Resources/curve.jpg"];

NSRect rect = NSMakeRect(0, 0, windowRect.size.width,windowRect.size.height-TITLEBAR_HEIGHT);
[imageView setFrame:rect];
[imageView setImageScaling:NSScaleToFit];
[imageView setImage:im];
[imageView display];

}

对于复制操作:

- (IBAction)copy:sender {
NSImage *image = [imageView image];
if (image != nil) {
NSPasteboard *pasteboard = [NSPasteboard generalPasteboard];
[pasteboard clearContents];
NSArray *copiedObjects = @[image];
[pasteboard writeObjects:copiedObjects];
}
}

如果我用 jpeg 文件中的图像替换位图图像(在上面的 placeImage 中注释掉行),则任何窗口的复制都会成功。如有任何建议,我们将不胜感激。

堆栈帧如下所示:

0 _platform_memmove$VARIANT$Nehalem

1 个 vImageCopyBuffer

2 Copy_Convert

3 AnyToAnyBiock

4 vImageConvert_AnyToAny

5 ConvertBytesWithRange

6 _CGimagePuginWriteTIFF

7 CGimageOestinationFinalize

8 +[NSBitmaplmageRep(NSBitmaplmageFileTypeExtensions)representationOflmageRepslnArray:usingType:properties:]

9 -[NStmage TIFFRepresentationUsingCompression:factot:]

10 -[NSimage PasteboardPropertylistForType:]

11 -[NSPasteboard writeObjects:]

12 -[DataWindowController 副本:]

13 -[NSApplication sendAction:to:from:]

14 -[NSMenutem _corePerformAction]

15 -[NSCarbonMenulmpl PerformActionWithHighlightingForltemAtIndex:]

16 -[NSMenu PerformKeyEquivalent:]

17 -[NSApplication _handleKeyEquivalent:]

18 -[NSApplication sendEvent:]

19 -[NSApplication 运行]

20 NSApplicationMain

21主要

22开始

最佳答案

这显然是一个内存管理问题。有用的信息在这里: question 16094748

用 nil 初始化位图数据平面,然后显式复制数据似乎可以解决问题。

-(void) placeImage:(NSRect) theRect{
windowRect = theRect;
[[self window] setTitle:windowName];

NSBitmapImageRep* bitmap = [[NSBitmapImageRep alloc]
initWithBitmapDataPlanes: nil
pixelsWide: iBitmap.getwidth() pixelsHigh: iBitmap.getheight()
bitsPerSample: 8 samplesPerPixel: 4 hasAlpha: YES isPlanar:NO
colorSpaceName:NSCustomColorSpace
bytesPerRow: 4*iBitmap.getwidth()
bitsPerPixel: 32];

memcpy([bitmap bitmapData], iBitmap.getpixdata(), iBitmap.getheight()*iBitmap.getwidth()*4);

NSImage* im = [[NSImage alloc] initWithSize:NSMakeSize(iBitmap.getwidth(), iBitmap.getheight())];
[im addRepresentation:bitmap];

//NSImage* im = [[NSImage alloc] initByReferencingFile:@"./Contents/Resources/curve.jpg"];

NSRect rect = NSMakeRect(0, 0, windowRect.size.width,windowRect.size.height-TITLEBAR_HEIGHT);
[imageView setFrame:rect];
[imageView setImageScaling:NSScaleToFit];
[imageView setImage:im];
[imageView display];

}

关于objective-c - 在 cocoa osx 中将位图图像复制到粘贴板时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20496822/

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