Frage

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?

War es hilfreich?

Lösung

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.

Andere Tipps

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];
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top