2013-12-10 75 views
2

我在寫一個計算位圖圖像的圖像處理應用程序。它將它們顯示在多個窗口中,我希望能夠將圖像複製到粘貼板。每個窗口都有自己的控制器(NSWindowController的子類)和imageView(NSImageView的子類)。將位圖圖像複製到Cocoa的粘貼板時遇到問題

我的代碼將成功地從最近顯示的窗口中複製圖像,但是當我從先前顯示的窗口中複製時,會給出EXC_BAD_ACCESS(代碼1)。

在用於設置位圖圖像的窗口控制器中的代碼是這樣的:

-(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圖像替換位圖圖像文件(註釋掉上面的line in placeImage),那麼複製對任何窗口都是成功的。任何建議,將不勝感激。

的棧幀是這樣的:

0 _platform_memmove $ VARIANT $的Nehalem

1 vImageCopyBuffer

2 Copy_Convert

3 AnyToAnyBiock

4 vImageConvert_AnyToAny

5ConvertBytesWithRange

6 _CGimagePiuginWriteTIFF

7 CGimageOestinationFinalize

8 + [NSBitmaplmageRep(NSBitmaplmageFileTypeExtensions)representationOflmageRepslnArray:usingType:性質:]

9 - [NStmage TIFFRepresentationUsingCompression:factot:]

10 - [NSimage pasteboardPropertylistForType:]

11 - [NSPasteboard writeObjects:]

12 - [DataWindowController複製:]

13 - [的NSApplication sendAction:爲:從:]

14 - [NSMenultem _corePerformAction]

15 - [NSCarbonMenulmpl performActionWithHighlightingForltemAtlndex:]

16 - [NSMenu performKeyEquivalent:]

17 - [的NSApplication _handleKeyEquivalent:]

18 - [的NSApplication的SendEvent:]

19 - [的NSApplication運行]

20 NSApplicationMain

21主要

22開始

回答

1

這顯然是一個內存管理問題。有用的信息在這裏: 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]; 

}