6
-(void)drawRect : (NSRect)rect 
{    
        imgRect.orgin = NSZeroPoint;

        imgRect.size = [appleImage size];

        drawRect = [self bounds];

        [appleRect drawInRect:appleRect     
                     fromRect:imgRect     
                     operation:NSCompositeSourceOver     
                     fraction:1.0];
        ...     
}

I'm trying this but it's not easy and driving me crazy.

How to change NSRect type code to CGRect type code ?

plz help!

ps. i already know.. NSRect -> CGRect , NSZeroPoint -> CGRectZero

Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
kevin
  • 69
  • 1
  • 1
  • 2
  • possible duplicate of [Is there any difference between a CGRect and an NSRect?](http://stackoverflow.com/questions/2151027/is-there-any-difference-between-a-cgrect-and-an-nsrect) – Adam Wright Jun 22 '11 at 12:58
  • 2
    There's nothing in your question that requires a conversion to CGRect. `-[NSImage drawInRect:fromRect:operation:fraction:]` takes an NSRect, which you appear to already have. – Peter Hosey Jun 22 '11 at 19:56
  • The blog post found here http://www.dribin.org/dave/blog/archives/2007/03/15/rect_conversion/ seems to cover this topic quite well. – Paul Tiarks Jun 22 '11 at 13:03
  • Check out this more updated answer... http://stackoverflow.com/a/25091800/4844059 – Whitney Foster Mar 02 '16 at 02:15

4 Answers4

20

Just use these 2 Cocoa (no iOS) functions:

CGRect NSRectToCGRect(NSRect nsrect);
NSRect NSRectFromCGRect(CGRect cgrect);

Have a look here to deepen the topic: http://cocoadev.com/CGRect

IlDan
  • 6,851
  • 3
  • 33
  • 34
  • @NicolasMiari ..hello from '5 years later' - and the link still working :D (though I agree in general!) – ATV Jun 22 '16 at 18:35
5

If you encounter this in iOS, such as with UIKeyboardFrameEndUserInfoKey, it's important to know that NSRect is a subclass of NSValue.

if let rectValue = notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue {
    let rect = rectValue.CGRectValue()
    // Use your CGRect
}
Holly
  • 5,270
  • 1
  • 24
  • 27
3

Assuming you're using Apple's Foundation and not GNUStep or something else odd...


Swift

In Swift, NSRect is just a typealias of CGRect (NSGeometry.swift line 449-ish), so you can simply use either in place of the other anywhere.

let nsTest1 = NSRect(x: 1, y: 2, width: 3, height: 4)
let cgTest1 = CGRect(x: 1, y: 2, width: 3, height: 4)
let cgTest2 = CGRect(x: 100, y: 200, width: 300, height: 400)

print(nsTest1 == cgTest1) // true


func takeCgRect(_ rect: CGRect) {
    print("A CoreGraphics rectangle at \(rect.origin) with size \(rect.size)")
}


func takeNsRect(_ rect: NSRect) {
    print("A NeXTStep rectangle at \(rect.origin) with size \(rect.size)")
}


takeCgRect(nsTest1) // A core graphics rectangle at (1.0, 2.0) with size (3.0, 4.0)
takeNsRect(cgTest2) // A NeXTStep rectangle at (100.0, 200.0) with size (300.0, 400.0)

Objective-C

In Objective-C, NSRect is just a typedef of CGRect (NSGeometry.h line 26-ish), so you can simply use either in place of the other anywhere.

void takeCgRect(CGRect rect) {
    NSLog(@"A CoreGraphics rectangle at %@ with size %@", NSStringFromCGPoint(rect.origin), NSStringFromCGSize(rect.size))
}


void takeNsRect(NSRect rect) {
    NSLog(@"A NeXTStep rectangle at %@ with size %@", NSStringFromPoint(rect.origin), NSStringFromSize(rect.size))
}



// ELSEWHERE:

NSRect nsTest1 = NSMakeRect(1, 2, 3, 4)
CGRect cgTest1 = CGRectMake(1, 2, 3, 4)
CGRect cgTest2 = CGRectMake(100, 200, 300, 400)

NSLog(@"%@", @(nsTest1 == cgTest1)) // 1


takeCgRect(nsTest1) // A core graphics rectangle at {1.0, 2.0} with size {3.0, 4.0}
takeNsRect(cgTest2) // A NeXTStep rectangle at {100.0, 200.0} with size {300.0, 400.0}
Ky -
  • 30,724
  • 51
  • 192
  • 308
  • 1
    https://developer.apple.com/documentation/foundation/1391329-nsmakerect `NSMakeRect` instead of `NSRectMake` – vaughan Dec 16 '20 at 16:22
  • 1
    LOL I totally just copied the CG ones and changed CG to NS. That's what I get for expecting consistency from Apple APIs Thanks, @vaughan! – Ky - Dec 16 '20 at 19:35
0

SWIFT 5

It works for me. Use 'if' before let.

            let keyboardFrame = (userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue
            print("\(String(describing: keyboardFrame?.height))")
Md Jahirul Islam
  • 211
  • 2
  • 10