• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Cropping an NSBitmapImageRep to a given NSRect?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Cropping an NSBitmapImageRep to a given NSRect?


  • Subject: Cropping an NSBitmapImageRep to a given NSRect?
  • From: Daniel Thorpe <email@hidden>
  • Date: Mon, 21 Apr 2008 12:43:14 +0100

Hello everyone,

I can't seem to get this to work, yet it seems like it should be so easy. I have an NSBitmapImageRep, and I want to crop it to a given NSRect. The code I've attempted so far is this:

// Create an NSImage for the current image rep
NSImage *source = [[NSImage alloc] init];

// Add the image rep to the source
[source addRepresentation:imageRep];

// Let's just write this to file
[[source TIFFRepresentation] writeToFile:[NSString stringWithFormat:@"%@_source.tiff ", label] atomically:YES];

self.extent = [imageRep nonZeroBounds]; // This is our NSRect, computed using a category method of NSBitmapImageRep

NSImage *target = [[NSImage alloc] initWithSize:extent.size];

// Set the target as the current drawing context
[target lockFocus];
// Composite a rectange of the source to a point in the target
[[NSGraphicsContext currentContext] setShouldAntialias:NO];
[source compositeToPoint:NSZeroPoint fromRect:extent operation:NSCompositeSourceIn fraction:1.0];
[target unlockFocus];


// Let's just write this to file
[[target TIFFRepresentation] writeToFile:[NSString stringWithFormat:@"%@_cropped.tiff ", label] atomically:YES];


I've not really used Quartz before, but this seems to be how all these methods work. Although it's not working, the cropped image is the right size, but is just black and doesn't contain the right rect of the source image... what am I doing wrong?

As always, the help from people on the list is really appreciated, thanks!

Cheers
Dan

_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: Cropping an NSBitmapImageRep to a given NSRect?
      • From: Graham Cox <email@hidden>
  • Prev by Date: Re: Trying to calculate a running total using Core Data
  • Next by Date: Re: Complex data for webservices
  • Previous by thread: Re: Trying to calculate a running total using Core Data
  • Next by thread: Re: Cropping an NSBitmapImageRep to a given NSRect?
  • Index(es):
    • Date
    • Thread