另一个IKImageView问题:复制区域

问题描述:

我试图使用IKImageView的选择和复制功能。如果你想要做的只有一个图像应用程序,选择一个部分并将其复制到剪贴板,这很容易。您将复制菜单选择设置为第一个响应者的副本:(id)方法,并且魔术般地一切正常。另一个IKImageView问题:复制区域

但是,如果你想要更复杂的东西,比如你想复制一些其他操作的一部分,我似乎无法找到这样做的方法。

IKImageView似乎没有复制方法,它似乎没有一个方法,甚至会告诉你选定的矩形!现在

我已经通过Hillegass的书了,所以我明白是怎么剪贴板工作,只是没有如何获得图像的部分缩小视图的...

,我开始想,我在IKImageView的基础上犯了一个错误,但是它是建立在Preview上的(或者我读过的),所以我认为它必须是稳定的......无论如何,现在已经太晚了,我也是在这个深处重新开始...

因此,除了不使用IKImageView,关于如何手动将选择区域复制到剪贴板的任何建议吗?

编辑实际上,我已经找到了副本(ID)的方法,但是当我打电话吧,我得到

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

这显然当我经历了第一代做一个正常的拷贝不会发生响应者...我了解错误信息,但我不知道它从哪里得到这些参数...

有没有什么方法可以追踪这一点,看看这是怎么回事?一个调试器将不会帮助,原因很明显,以及我在Mozilla中这样做,所以调试器不是一个选项无论如何...

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

我认为这是剪贴板副本的原因是,在另一个项目中,我从IKImageView直接从编辑菜单复制到剪贴板,它只是发送副本:(id)到firstResponder,但我没有真正确定firstresponder不会做什么用......

编辑3看来,CGBitmapContextCreate误差由[imageView image]来这,奇怪的是,IS一个记录方法。

这可能是因为我把图像放在那里用setImage:(id)方法,将它传递给一个NSImage * ...有没有其他更聪明的方法让NSImage进入IKImageView?

+0

你绝对可以调试插件在外部应用程序,如火狐浏览器。只需在Xcode项目中创建一个自定义可执行文件,并将其指向要调试的应用程序。我已经使用Safari,Dreamweaver,Adobe GoLive和其他软件成功完成了此项工作。 – 2010-03-25 01:11:36

+0

“我发现我找到的'copy:(id)'方法可能是复制VIEW而不是将图像的块复制到剪贴板中......”不,你正在考虑'copy'(注意没有参数),它返回对象的一个​​副本,并且不能在视图上工作。 'copy:'(一个参数)'是一个动作,告诉接收者(视图)将其选择复制到剪贴板。 – 2010-03-25 08:01:44

中的方法做了其他所有-copy:方法:它将当前选择复制到剪贴板。但是,由于某种原因,它在IKImageView中作为私人方法实施。

您只需直接调用它:无论是当前选择到剪贴板

[imageView copy:nil]; 

这将复制。

我不认为有一种方法可以使用公共方法直接访问IKImageView中当前选择的图像内容,这是一个很好的候选bug报告/功能请求。

你可以,但是,使用私有方法-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 
+0

不要忘记测试'imageView'是否响应'selectionRect'。由于这是一种私人方法,因此Apple可以随时重命名,更改或删除该方法,如果无条件地发送该消息,则会破坏您的代码。 – 2010-03-25 16:14:02

+0

当我尝试[imageView copy:nil]我得到 :CGBitmapContextCreate:不受支持的参数组合:8个整数位/组件; 16位/像素; 1分量色彩空间; kCGImageAlphaPremultipliedLast; 2624字节/行。 – 2010-03-25 18:00:25

+0

当我尝试你的方法时,我得到同样的错误... – 2010-03-25 18:27:15

好了,所以副本:零失败,和[imageView图像]失败,但事实证明,我从第一次将其添加到视图中时获得了另一个NSImage副本,所以我可以做到这一点。此外,CGImageCreateWithImageInRect期望CGImageRef不是NSImage *,所以我不得不做一些转换。

此外,由于某些原因,选择矩形被翻转,无论是底部起始,图像是顶部,或其他方式,所以我不得不翻转它。

,由于某种原因,编译器突然开始抱怨的NSRect不是同一类型的CGRect(这意味着它突然从去32到64位或东西...不知道为什么...)

总之,这里是我的selectedImage的副本:

- (NSImage*)selectedImage 
{ 
    //get the current selection 
    CGRect selection = flipCGRect(imageView, [imageView selectionRect]); 

    //get the portion of the image that the selection defines 
    struct CGImage * full = [[doc currentImage] CGImageForProposedRect: NULL context: NULL hints: NULL]; 

    CGImageRef selectedImage = CGImageCreateWithImageInRect(full, 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; 

} 

我写flipCGRect和[DOC currentImage]返回一个NSImage中* ...