0

I'm new to cocoa and programming, sorry if this is something basic.

I'm using ARC. I have an NSImageView component, which is controlled by DropZone class. I drag & drop an image into it, but once I try to call in the scalling method I got, it tells me that the ": ImageIO: CGImageSourceCreateWithData data parameter is nil" I assume I'm doing something wrong, just don't know what yet.

Here's my method in 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];
}

The DropZone.h

@interface DropZone : NSImageView <NSDraggingDestination>{
  NSImage *image;
}
- (void)scaleIcons:(NSString *)outputPath;
@end

And here's how I call it it my 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];
}

the image get's pulled from the 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;
}

For some reason my "image" gets lost. Could somebody help?

Rokas
  • 57
  • 1
  • 5

2 Answers2

1

The problem is that you're creating a new instance of DropZone in your app delegate, but I'm assuming that you created the image view in IB and changed its class to DropZone. Is that correct? If so, you need to have an IBOutlet in the app delegate connected to the image view in IB, and have this:

    [self.iv scaleIcons:tempDir];

where iv is my IBOutlet.

rdelmar
  • 103,982
  • 12
  • 207
  • 218
  • You are right. I added NSImageView to my .xib and I changed it's class to "DropZone". I added the outlet to the app delegate from image view, but now it says: " Unknown type name 'DropZone'" What am I doing wrong here? @property (weak) IBOutlet DropZone *myImageView; – Rokas Jan 09 '13 at 00:32
  • @Rokas You need to add an "@class DropZone;" at the top of the .h file (above the "@interface line"). – rdelmar Jan 09 '13 at 00:34
  • Thanks a lot for your help! Learned a lot! I get the idea what was wrong :) Except the fact that I need to look into my scaling algorithm, it looks like, my program now is saving images!:) Thanks again! – Rokas Jan 09 '13 at 00:45
0

Without knowing where "image" comes from, I would say your problem is in these lines...

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

You should just grab the image from the imageView...

anImage = [myImageView image];
regulus6633
  • 18,848
  • 5
  • 41
  • 49
  • Just tried your approach and the error is still the same ": ImageIO: CGImageSourceCreateWithData data parameter is nil". Added the method, which gets the image from pasteboard to the question. – Rokas Jan 09 '13 at 00:12