Re: Cropping an NSBitmapImageRep to a given NSRect? [Solved]
Re: Cropping an NSBitmapImageRep to a given NSRect? [Solved]
- Subject: Re: Cropping an NSBitmapImageRep to a given NSRect? [Solved]
- From: Daniel Thorpe <email@hidden>
- Date: Mon, 21 Apr 2008 14:46:31 +0100
Right, well, I've solved my problem, but using a completely different
method. I still don't know/understand how to crop an NSImage to a
smaller NSRect within the image's bounds.
However, this is what I've done to solve this...
// imageRep is my NSBitmapImageRep which contains the original image,
// extent is now a NSRect that defines a rectangle within imageRep
that we wish to crop to
self.extent = [imageRep nonZeroBounds];
// Target is an NSImage which we're going to create
NSImage *target = [[NSImage alloc] initWithSize:extent.size];
[target addRepresentation:[imageRep cropToRect:extent]];
And this uses a category method on NSBitmapImageRep, called
cropToRect: which is as follows:
- (NSBitmapImageRep *)cropToRect:(NSRect)rect {
/* This method retuns a NSBitmapImage containing the
rectangle defined by the input bounds */
CGImageRef cgImg = CGImageCreateWithImageInRect([self CGImage],
NSRectToCGRect(rect));
NSBitmapImageRep *result = [[NSBitmapImageRep alloc]
initWithCGImage:cgImg];
CGImageRelease(cgImg);
return [result autorelease];
}
Anyway, this works a treat, and for my needs is actually pretty good,
as I'm just manipulating NSBitmapImageReps, not NSImages.
Thanks for your help Graham!
Cheers
Dan
p.s. I've just seen your most recent email, so I'll try using the
drawAtPoint using a negative origin.
On 21 Apr 2008, at 13:41, Graham Cox wrote:
Hard to tell from the code posted. Maybe extent is incorrect?
You can just draw the source imageRep directly - you don't have to
wrap it in an image, and given that you just want a straight copy
it'll be simpler too. Use [imageRep drawAtPoint:...] This may not
have a bearing on your problem but the simpler the code, the easier
to debug :)
Having different colourspaces shouldn't matter - the drawing
operation will convert the image as necessary.
G.
On 21 Apr 2008, at 10:23 pm, Daniel Thorpe wrote:
Thanks Graham, I've just tried NSCompositeCopy, still doesn't work,
no change...
One thing that I've noticed though, is that the source is a
grayscale image, but the image that I'm creating is RGB. Should I
perhaps be creating a new bitmap image rep in the target (of the
same colorSpace) before calling the compositeToPoint method?
Cheers
Dan
On 21 Apr 2008, at 12:57, Graham Cox wrote:
Oops, I meant NSCompositeCopy
g.
On 21 Apr 2008, at 9:55 pm, Graham Cox wrote:
On 21 Apr 2008, at 9:43 pm, Daniel Thorpe wrote:
[source compositeToPoint:NSZeroPoint fromRect:extent
operation:NSCompositeSourceIn fraction:1.0];
Try using NSCompositeSourceCopy here instead.
G.
_______________________________________________
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
_______________________________________________
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