另IKImageView问题:复制一个区域
-
21-09-2019 - |
题
我试图使用IKImageView的选择和复制功能。如果你想要做的是有图像的应用程序,选择一个部分,并将其复制到剪贴板,这很容易。您复制菜单中选择设置为第一个响应者的副本:(ID)方法,并奇迹般地一切正常
不过,如果你想更复杂的东西,比如你要复制一些其他操作的一部分,我似乎无法找到这样做的方法。
IKImageView似乎不具有复制方法,它似乎并不有一种方法,即使将告诉你所选择的矩形!
我已经通过Hillegass的书了,所以我明白是怎么剪贴板工作,只是没有如何获得图像的部分缩小视图的......
现在,我开始想,我在IKImageView立足我的项目犯了一个错误,但它是什么预览是建立在(或因此我读过),所以我估计它必须是稳定的...反正,现在为时已晚,我在这个太深从头再来......
所以,比不使用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)的方法,我发现可以复制的视图而不是复制的图像的块到剪贴板,这是我所需要的
我还以为是拷贝到剪贴板的是,在另一个项目中,我从一个IKImageView从编辑菜单直剪贴板复制的原因,它只是将复印(ID)的firstResponder,但我“M实际上并不知道什么是firstresponder确实与它...
修改3 强>看来,CGBitmapContextCreate误差从[imageView image]
其中,奇怪的是, IS 一个记录方法来了。
这是可能的,这种情况正在发生,因为我把图像中有一个setImage:(ID)方法,传递一个NSImage中* ...有没有得到一个NSImage中到IKImageView的一些其他的,更聪明的方法?
解决方案
在-copy:
的IKImageView
方法所做的所有其它-copy:
方法完成:其拷贝当前选择到剪贴板。据,然而,实现为在IKImageView
由于某些原因的私有方法。
您只需直接调用它:
[imageView copy:nil];
此将复制任何当前选择到剪贴板中。
我不认为有办法直接访问使用公共方法IKImageView
当前选择的图像内容,这是一个错误报告/功能请求一个很好的候选人。
可以,但是,使用私有方法-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
其他提示
好了,所以复制:无失败,并且[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中* ...