initializing images with view contents, then displaying those images instead of the view
initializing images with view contents, then displaying those images instead of the view
- Subject: initializing images with view contents, then displaying those images instead of the view
- From: David Harper <email@hidden>
- Date: Fri, 12 Oct 2007 16:53:35 -0400 (EDT)
Hi,
In an effort to improve efficiency in my current project, I have decided to create NSImages to represent the contents of some of the subviews in the application. This is a somewhat well-documented process that I've read about in several different places.
These views contain dynamically positioned text, and when many of them are on screen and thousands of individual characters are being drawn using layoutmanagers etc, the application can gradually slow down to unacceptable levels (~4 frames per second or lower). The solution I've come up with so far works, except that the image being drawn in the view does not exactly match the one which was copied using the view's contents... Its width and height seem to vary by 1-2 pixels.
This may have something to do with the fact that the view frame sometimes has non-integer width and height. I'm not sure if NSImages are required to have integer NSSize values...
the code for creating the view's image (sorry about the bad code formatting):
[self lockFocus];
NSBitmapImageRep *tempBitmap = [[[NSBitmapImageRep alloc] initWithFocusedViewRect: imgFrame] autorelease];
[self unlockFocus];
NSImage *tempImage = [[[NSImage alloc] initWithSize:imgFrame.size] autorelease],
*resultImage = [[[NSImage alloc] initWithSize:imgFrame.size] autorelease];
[tempImage addRepresentation: tempBitmap];
if (inAlpha == 1.0)
return tempImage;
[resultImage lockFocus];
[tempImage drawInRect: (NSRect){0, 0, imgFrame.size.width, imgFrame.size.height}
fromRect: (NSRect){0, 0, imgFrame.size.width, imgFrame.size.height}
operation: NSCompositeSourceOver
fraction: 1.0];
[resultImage unlockFocus];
return resultImage;
------------
for some reason, the image created with the code above is vertically flipped! So I manually unflip it with an affine transformation using the drawing code below:
[[NSGraphicsContext currentContext] saveGraphicsState];
NSAffineTransform *t = [NSAffineTransform transform];
[t scaleXBy:1.0 yBy:-1.0];
[t translateXBy:0.0 yBy:-[myImage size].height];
[t concat];
[myImage drawInRect: (NSRect){0,0,[myImage size].width, [myImage size].height}
fromRect: (NSRect){0,0,[myImage size].width, [myImage size].height}
operation: NSCompositeSourceOver
fraction: 1.0];
[[NSGraphicsContext currentContext] restoreGraphicsState];
If anyone has any suggestions I would greatly appreciate your input.
Thanks,
-Dave H.
_______________________________________________
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