我是新來的可可和編程,抱歉,如果這是基本的東西。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