gpt4 book ai didi

cocoa nsimage 未保存

转载 作者:行者123 更新时间:2023-12-03 17:55:33 25 4
gpt4 key购买 nike

我是 cocoa 和编程的新手,如果这是基本的东西,我很抱歉。

我正在使用 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;
}

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

最佳答案

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

    [self.iv scaleIcons:tempDir];

其中 iv 是我的 IBOutlet。

关于 cocoa nsimage 未保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14224644/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com