cocoa nsimage无法保存
我是可可和编程新手,如果这是一些基本的东西,我很抱歉 我用的是ARC。我有一个NSImageView组件,它由DropZone类控制。我将一个图像拖放到其中,但一旦我尝试调用我得到的scaling方法,它告诉我“:ImageIO:CGImageSourceCreateWithData data参数为nil”我假设我做错了什么,只是还不知道是什么 这是我在DropZone.m中的方法cocoa nsimage无法保存,cocoa,variables,nsimage,Cocoa,Variables,Nsimage,我是可可和编程新手,如果这是一些基本的东西,我很抱歉 我用的是ARC。我有一个NSImageView组件,它由DropZone类控制。我将一个图像拖放到其中,但一旦我尝试调用我得到的scaling方法,它告诉我“:ImageIO:CGImageSourceCreateWithData data参数为nil”我假设我做错了什么,只是还不知道是什么 这是我在DropZone.m中的方法 - (void)scaleIcons:(NSString *)outputPath{ NSImage *anIma
- (void)scaleIcons:(NSString *)outputPath{
NSImage *anImage;
NSSize imageSize;
NSString *finalPath;
anImage = [[NSImage alloc]init];
anImage = image;
imageSize = [anImage size];
imageSize.width = 512;
imageSize.height = 512;
[anImage setSize:imageSize];
finalPath = [outputPath stringByAppendingString:@"/icon_512x512.png"];
NSData *imageData = [anImage TIFFRepresentation];
NSBitmapImageRep *rep = [NSBitmapImageRep imageRepWithData:imageData];
NSData *dataToWrite = [rep representationUsingType:NSPNGFileType properties:nil];
[dataToWrite writeToFile:finalPath atomically:NO];
}
DropZone.h
@interface DropZone : NSImageView <NSDraggingDestination>{
NSImage *image;
}
- (void)scaleIcons:(NSString *)outputPath;
@end
已从pastebaord中提取图像:
- (BOOL)performDragOperation:(id<NSDraggingInfo>)sender{
if ([NSImage canInitWithPasteboard:[sender draggingPasteboard]]) {
image = [[NSImage alloc]initWithPasteboard:[sender draggingPasteboard]];
[self setImage:image];
[self setImageAlignment: NSImageAlignCenter];
}
return YES;
}
-(BOOL)性能跟踪操作:(id)发送方{
如果([NSImage canInitWithPasteboard:[发送方拖动PasteBoard]]){
image=[[NSImage alloc]initWithPasteboard:[发送方拖动PasteBoard]];
[自我设置图像:图像];
[自设置图像对齐:NSImageAlignCenter];
}
返回YES;
}
由于某种原因,我的“形象”消失了。有人能帮忙吗?如果不知道“图像”是从哪里来的,我会说你的问题就在这些方面
anImage = [[NSImage alloc]init];
anImage = image;
你应该从imageView中抓取图像
anImage = [myImageView image];
问题是您正在应用程序委托中创建DropZone的新实例,但我假设您在IB中创建了图像视图,并将其类更改为DropZone。对吗?如果是这样,您需要在app delegate中有一个IBOutlet连接到IB中的图像视图,并具有以下功能:
[self.iv scaleIcons:tempDir];
其中iv是我的IBOutlet。这条线是什么:anImage=image;图像来自哪里?图像来自粘贴板。将方法添加到问题中。刚刚尝试了您的方法,错误仍然相同:“ImageIO:CGImageSourceCreateWithData数据参数为nil”。添加了从粘贴板获取图像的方法。你是对的。我将NSImageView添加到我的.xib中,并将其类更改为“DropZone”。我从图像视图将插座添加到应用程序代理,但现在它显示:“未知类型名称‘DropZone’”,我在这里做错了什么@属性(弱)IBOutlet DropZone*myImageView@Rokas您需要在.h文件顶部(在“@接口行”上方)添加一个“@class DropZone;”。非常感谢您的帮助!学到了很多!我知道是什么错了:)除了我需要研究我的缩放算法之外,看起来,我的程序现在正在保存图像!:)再次感谢!
[self.iv scaleIcons:tempDir];