3

I'm pretty new to drawing images in general with Objective C. I have done image drawing in iPhone development, but now I want to do so on a Mac. In short, what is the Mac equivalent of the iPhone code below?

- (void) drawRect: (CGRect) rect
{
    [super drawRect:rect];

    UIImage *anotherimage = [UIImage imageNamed:@"alert.png"];
    CGPoint imagepoint = CGPointMake(10,0);
    [anotherimage drawAtPoint:imagepoint];

}
Ky -
  • 30,724
  • 51
  • 192
  • 308
Scsm326
  • 70
  • 1
  • 7

1 Answers1

3

This should work, assuming you don't want any image transparency and composition effects.

-(void)drawRect:(NSRect)dirtyRect
{
    [super drawRect:dirtyRect];
    NSImage *anotherImage = [NSImage imageNamed:@"alert.png"];
    [anotherImage drawAtPoint:NSMakePoint(10,0) fromRect:rectToDrawImage operation:NSCompositeCopy fraction:1.0];
}
lead_the_zeppelin
  • 2,017
  • 13
  • 23
  • 1
    You probably know this, but you might want to move creation of the image out of **drawRect:** which has to be as clean and fast as possible. – lead_the_zeppelin Nov 03 '13 at 23:49
  • 1
    Thanks a lot. I needed to make a few changes, but it worked in the end. I added in NSMakeRect(0,0,[anotherImage size].width, [anotherImage size].height) – Scsm326 Nov 05 '13 at 22:53
  • NSImage does not have a "frame" property, or so says Xcode. – Will May 16 '14 at 18:18