Implement drag from NSImageView and save image to a file

后端 未结 2 1414
执笔经年
执笔经年 2021-01-06 05:09

I\'ve studied Apple\'s example: CocoaDragAndDrop. I understand how to drag a image to NSImageView and drag an image between views. However, I still don\'t known how to drag

相关标签:
2条回答
  • 2021-01-06 05:53

    I finally found out how to do it. And I wrote a demo on Github

    0 讨论(0)
  • 2021-01-06 06:07
    -(IBAction)saveImageButtonPushed:(id)sender
    {
        NSLog(@"saveImageButtonPushed");
    
        NSBitmapImageRep *rep;
        NSData *data;
        NSImage *image;
    
        [self lockFocus];
        rep = [[NSBitmapImageRep alloc] initWithFocusedViewRect:[self frame]];
        [self unlockFocus];
    
        image = [[[NSImage alloc] initWithSize:[rep size]] autorelease];
        [image addRepresentation:rep];
    
        data = [rep representationUsingType: NSPNGFileType properties: nil];
            //save as png but failed
        [data writeToFile: @"asd.png" atomically: NO];
    
            //save as pdf, succeeded but with flaw
        data = [self dataWithPDFInsideRect:[self frame]];
        [data writeToFile:@"asd.pdf" atomically:YES];
    }
    //......
    @end
    
    0 讨论(0)
提交回复
热议问题