Resolved Origin being ignored in cropped image.

Discussion in 'Mac Programming' started by aperantos, Sep 1, 2011.

  1. aperantos, Sep 1, 2011
    Last edited: Sep 3, 2011

    aperantos macrumors member

    Joined:
    Feb 18, 2008
    #1
    I want to crop some tall images so that they are three quarters their width, keeping the top part of them.

    Although the images are being cropped to the correct ratio, the position from which the crop is taken seems to be totally random, it can be from the top, middle, or bottom.

    Here is the code I am using:

    Code:
    NSImage *originalImage = [[NSImage alloc] initWithContentsOfURL:[NSURL fileURLWithPath:previewFile]];
    NSSize imageSize = [originalImage size];
    NSRect newSize = NSMakeRect(0, 0, imageSize.width, imageSize.width / 1.333);
    NSImage *croppedImage = [[NSImage alloc] initWithSize:imageSize.size];
    [croppedImage lockFocus];
    [originalImage drawInRect:newSize fromRect:newSize operation:NSCompositeCopy fraction:1.0];
    [croppedImage unlockFocus];	
    What am I missing, surely the 0, 0 in the NSRect means it will copy from the top left of the source to the top left of the target?

    Thanks in advance for any assistance.
     
  2. Shawnpk macrumors 6502

    Joined:
    Jan 13, 2011
    Location:
    Los Angeles, CA
    #2
    I'm not in front of my computer right now, but if I'm not mistaken, a NSMakeRect takes an x, y, width and height. I notice you have an x, y, width and width.
     
  3. aperantos thread starter macrumors member

    Joined:
    Feb 18, 2008
    #3
    That is deliberate so the cropped size will have a 4:3 ratio. So I am passing the width divided by 1.333 (4/3) as the height. That part is working fine, the only issue is the origin of the crop which I seem unable to control.
     
  4. xStep macrumors 68000

    Joined:
    Jan 28, 2003
    Location:
    Less lost in L.A.
    #4
    "I want to crop some tall images so that they are three quarters their width, keeping the top part of them."

    Just so we are clear, you want the upper part of an image that is equal to 1/3 of the width of the image? Don't forget to account for oddities like 600 wide by 100 high. Also, why are you dividing the width by 1.333 instead of (1.0/3.0). Given my understanding, that isn't making sense to me.

    I don't believe you copied and posted code from working source. The give away is that NSSize is a structure and doesn't have a size attribute, but does have width and height attributes. So, imageSize.size doesn't exist, for starters.

    I also suspect you may be having an issue with the origin of the image. I'd expect that you will be picking from the bottom up because I believe the origin is the lower left corner in NSImages. Read the Cocoa Drawing Guide regarding origin.

    Your Y origin value is set to zero, so I can't see a reason you'd be getting images between the top and bottom of the original image. I'd expect that you'd be starting from the bottom and if your height value is positive, then capturing pixels going up from the bottom. So the lower potion as I've indicated above.

    Am I missing something?
     
  5. aperantos thread starter macrumors member

    Joined:
    Feb 18, 2008
    #5
    Thank you, that was the problem. I knew it had to be something I was overlooking, I must have confused it with NSBitmapImageRep from an earlier attempt which uses the top left as the origin.

    Incidentally, the reason I am dividing the width by 1.333 is that I want a 4:3 ratio (the height being three-quarters of the width) and so dividing by 4/3rds gives that. I could have multiplied by 3/4 (0.75) for the same effect, but I just instinctively went with the division approach. Also I know in advance the image will be taller than it is wide, so there was no need to check for that.

    And the error in the code was because I took posting here as a chance to tidy up my variable names, and made a typo which I later caught where I meant to use newSize.size.

    Anyway, everything is working now so thanks again for resolving my problem.
     

Share This Page