Re: NSImage lock/unlock slow
Re: NSImage lock/unlock slow
- Subject: Re: NSImage lock/unlock slow
- From: "Ken Ferry" <email@hidden>
- Date: Sat, 1 Dec 2007 18:28:36 -0800
> To my knowledge there is no way to get around this?????
Aside from other work you aren't interested in, this is making an
extra copy of the image data. -lockFocus is going to end up drawing
your original bitmap into a buffer used to hold your new drawing, and
-[NSBitmapImageRep initWithFocusedViewRect:] is going to create a
third buffer.
Since you're after specifically a bitmap, it's better to draw directly
to a bitmap. Here's a related snippet from the 10.4 AppKit release
notes:
NSGraphicsContext *bitmapGraphicsContext = [NSGraphicsContext
graphicsContextWithBitmapImageRep:cacheBitmapImageRep];
[NSGraphicsContext saveGraphicsState];
[NSGraphicsContext setCurrentContext:bitmapGraphicsContext];
[[NSColor clearColor] set];
NSRectFill(NSMakeRect(0, 0, [cacheBitmapImageRep size].width,
[cacheBitmapImageRep size].height));
[NSGraphicsContext restoreGraphicsState];
Also, be careful! You never init'd your source image object. It's a
little bit surprising if an un-init'd image works on 10.5 at all, but
there's no guarantee it will in future OSes.
-Ken
On Dec 1, 2007 5:16 PM, <email@hidden> wrote:
> Hi
>
> I am trying to get an idea of what is fast efficient and so on.
>
> In my case the origin is a NSBitmapImage that I want cutout a portion
> from and scale that portion into a new NSBitmapImage.
>
>
> for (int i = 0; i < 10000; i++) {
> // Create a NSImage from NSBitmapImageRep
> NSImage *sourceImage = [NSImage alloc];
> [sourceImage addRepresentation:image];
> [sourceImage setFlipped:true];
>
> // Create a new NSImage
> NSRect targetRect = NSMakeRect(0, 0, 20, 20);
> NSImage *targetImage = [[NSImage alloc] initWithSize: targetRect.size];
> [targetImage setFlipped:true];
>
> [targetImage lockFocus];
> // Draw a part from source into target and stretch/shrink
> [sourceImage drawInRect:targetRect fromRect:NSMakeRect(371, 179, 10,
> 10) operation:NSCompositeCopy fraction:1.0];
>
> // Create a NSBitmapImageRep from locked NSImage
> NSBitmapImageRep *targetRep = [[NSBitmapImageRep alloc]
> initWithFocusedViewRect: NSMakeRect(0, 0, 20, 20)];
> [targetImage unlockFocus];
> [targetImage release];
> [sourceImage release];
> }
>
> When running this code over and over again. approx 50% of the time is
> spent in lock/unlock and the other 50% in the drawInRect command.
>
> I am a bit demotivated from the fact that the focus command is so
> expensive.
> To my knowledge there is no way to get around this?????
>
> THX
> _______________________________________________
>
> 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