objective-c - 另一个 IKImageView 问题 : copying a region

标签 objective-c cocoa imagekit ikimageview

我正在尝试使用 IKImageView 的选择和复制功能。如果您只想拥有一个带有图像的应用程序,选择一部分并将其复制到剪贴板,这很简单。您将复制菜单选择设置为第一响应者的 copy:(id) 方法,并且神奇地一切正常。

但是,如果你想要更复杂的东西,比如你想复制作为其他操作的一部分,我似乎找不到执行此操作的方法。

IKImageView 似乎没有复制方法,它似乎没有甚至可以告诉您所选矩形的方法!

我已经阅读了 Hillegass 的书,所以我了解剪贴板是如何工作的,只是不知道如何将图像的一部分从 View 中取出......

现在,我开始认为我在将我的项目基于 IKImageView 时犯了一个错误,但它是预览的基础(或者我读过),所以我认为它必须稳定......不管怎样,现在已经太晚了,我已经陷得太深,无法重新开始......

那么,除了不使用 IKImageView 之外,还有关于如何手动将选择区域复制到剪贴板的建议吗?

编辑实际上,我找到了 copy(id) 方法,但是当我调用它时,我得到了

 <Error>: CGBitmapContextCreate: unsupported parameter combination: 8 integer bits/component; 16 bits/pixel; 1-component color space; kCGImageAlphaPremultipliedLast; 2624 bytes/row.

当我通过第一响应程序进行正常复制时,显然不会发生这种情况...我理解错误消息,但我不确定它从哪里获取这些参数...

有什么方法可以追踪并了解这是如何发生的吗?由于显而易见的原因,调试器不会有帮助,而且我是在 Mozilla 中执行此操作,因此调试器无论如何都不是一个选项...

编辑2我发现我发现的 copy:(id) 方法可能是复制 VIEW 而不是将图像 block 复制到剪贴板,而这正是我所需要的。

我认为这是剪贴板副本的原因是,在另一个项目中,我直接从编辑菜单从 IKImageView 复制到剪贴板,它只是发送一个副本:(id) 到第一个响应者,但我我实际上不确定第一响应者用它做什么......

编辑 3 看来 CGBitmapContextCreate 错误来自 [imageView image],奇怪的是,一个已记录的方法。

发生这种情况可能是因为我用 setImage:(id) 方法将图像放在那里,并向其传递 NSImage*...是否有其他更聪明的方法将 NSImage 放入 IKImageView ?

最佳答案

IKImageView 中的 -copy: 方法执行所有其他 -copy: 方法的操作:它将当前选择内容复制到剪贴板。然而,出于某种原因,它在 IKImageView 中作为私有(private)方法实现。

直接调用即可:

[imageView copy:nil];

这会将当前选择的内容复制到剪贴板。

我认为没有办法使用公共(public)方法直接访问 IKImageView 中当前选择的图像内容,这是错误报告/功能请求的一个很好的候选者。

但是,您可以使用私有(private)方法-selectionRect来获取当前选择的CGRect,并使用它来提取图像的选定部分:

//stop the compiler from complaining when we call a private method
@interface IKImageView (CompilerSTFU)
- (CGRect)selectionRect
@end

@implementation YourController
//imageView is an IBOutlet connected to your IKImageView
- (NSImage*)selectedImage
{
    //get the current selection
    CGRect selection = [imageView selectionRect];

    //get the portion of the image that the selection defines
    CGImageRef selectedImage = CGImageCreateWithImageInRect([imageView image],(CGRect)selection);

    //convert it to an NSBitmapImageRep
    NSBitmapImageRep* bitmap = [[[NSBitmapImageRep alloc] initWithCGImage:selectedImage] autorelease];
    CGImageRelease(selectedImage);

    //create an image from the bitmap data
    NSImage* image = [[[NSImage alloc] initWithData:[bitmap TIFFRepresentation]] autorelease];

    //in 10.6 you can skip converting to an NSBitmapImageRep by doing this:
    //NSImage* image = [[NSImage alloc] initWithCGImage:selectedImage size:NSZeroSize];     
    return image;
}
@end

关于objective-c - 另一个 IKImageView 问题 : copying a region,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2496022/

相关文章:

iphone - Objective-C class_conformsToProtocol() 错误?

ios - 使用特定尺寸的图像对 GPUImageAmatorkaFilter 进行故障处理

objective-c - 重新分配父 PID

cocoa - 如何激活 Cocoa 帮助文件中的 TOC 按钮?

cocoa - NSURL.URLByResolvingSymlinksInPath 不适用于/tmp、/var。有解决方法吗?

cocoa - 如何确保 NSWindow 在代码块末尾重新绘制

objective-c - 如何启用 IKImageBrowserView 的特定项目上的拖动?

iphone - 全局整数赋值 - 需要左值作为赋值的左操作数

objective-c - NSStringFromSize([self intercellSpacing]) 的类型不兼容?