0

我是可可和编程的新手,对不起,如果这是基本的东西。

我正在使用ARC。我有一个 NSImageView 组件,它由 DropZone 类控制。我将图像拖放到其中,但是一旦我尝试调用我得到的缩放方法,它就会告诉我“:ImageIO:CGImageSourceCreateWithData数据参数为零”我认为我做错了什么,只是不要还知道什么。

这是我在 DropZone.m 中的方法

- (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

这就是我如何称它为我的 AppDelegate.m

- (IBAction)createIconButtonClicked:(id)sender {
NSFileManager *filemgr;
NSString *tempDir;

filemgr = [NSFileManager defaultManager];
tempDir = [pathString stringByAppendingString:@"/icon.iconset"];
NSURL *newDir = [NSURL fileURLWithPath:tempDir];
[filemgr createDirectoryAtURL: newDir withIntermediateDirectories:YES attributes: nil error:nil];
DropZone *myZone = [[DropZone alloc] init];
[myZone scaleIcons:tempDir];

NSAlert *alert = [[NSAlert alloc] init];
[alert setMessageText:@"Done!"];
[alert runModal];
}

图像是从 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;
}

由于某种原因,我的“形象”丢失了。有人可以帮忙吗?

4

2 回答 2

1

问题是您在应用程序委托中创建了 DropZone 的新实例,但我假设您在 IB 中创建了图像视图并将其类更改为 DropZone。那是对的吗?如果是这样,您需要在应用程序委托中有一个 IBOutlet 连接到 IB 中的图像视图,并具有以下功能:

    [self.iv scaleIcons:tempDir];

其中 iv 是我的 IBOutlet。

于 2013-01-09T00:18:14.177 回答
0

在不知道“图像”来自哪里的情况下,我会说你的问题出在这些方面......

anImage = [[NSImage alloc]init];
anImage = image;

您应该只从 imageView 中获取图像...

anImage = [myImageView image];
于 2013-01-09T00:02:32.383 回答