2013-01-08 134 views
0

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

由於某種原因我的「形象」迷路了。有人可以幫忙嗎?

+0

這條線是什麼:anImage = image;圖像從哪裏來? – rdelmar

+0

圖片來自粘貼板。在問題中添加了該方法。 – Rokas

回答

1

問題是,您在應用程序委託中創建了DropZone的新實例,但我假設您在IB中創建了圖像視圖並將其類更改爲DropZone。那是對的嗎?如果是這樣,你需要有一個IBOutlet在連接到IB的圖像視圖的應用程序委託,並有這樣的:

[self.iv scaleIcons:tempDir]; 

其中IV是我的IBOutlet中。

+0

你說得對。我將NSImageView添加到我的.xib中,並將其類改爲「DropZone」。 我從圖像視圖添加了應用程序委託的插座,但現在它說:「未知類型名稱'DropZone'」我在這裏做錯了什麼? @property(弱)IBOutlet DropZone * myImageView; – Rokas

+0

@Rokas你需要添加一個「@class DropZone;」位於.h文件的頂部(位於「@interface行」之上)。 – rdelmar

+0

非常感謝您的幫助!學到了很多!我知道什麼是錯誤的:)除了我需要看看我的縮放算法的事實,它看起來像,我的程序現在正在保存圖像!:)再次感謝! – Rokas

0

不知道這裏的「圖像」從何而來,我會說你的問題是在這些線路...

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

你應該只抓住從ImageView的圖像...

anImage = [myImageView image]; 
+0

剛剛嘗試過您的方法,錯誤仍然相同「:ImageIO:CGImageSourceCreateWithData數據參數爲零」。 添加了從粘貼板獲取圖像到問題的方法。 – Rokas