我是新来的可可和编程,抱歉,如果这是基本的东西。cocoa nsimage没有保存
我正在使用ARC。我有一个由DropZone类控制的NSImageView组件。我拖动&将一个图像放入它,但是一旦我尝试调用我得到的缩放方法,它告诉我“: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];
}
图像得到的从巴巴拉拉:
- (BOOL)performDragOperation:(id<NSDraggingInfo>)sender{
if ([NSImage canInitWithPasteboard:[sender draggingPasteboard]]) {
image = [[NSImage alloc]initWithPasteboard:[sender draggingPasteboard]];
[self setImage:image];
[self setImageAlignment: NSImageAlignCenter];
}
return YES;
}
由于某种原因我的“形象”迷路了。有人可以帮忙吗?
这条线是什么:anImage = image;图像从哪里来? – rdelmar
图片来自粘贴板。在问题中添加了该方法。 – Rokas