Len drawing image

I have an object that needs to be drawn in a graphical context on demand, however, the content takes time to render and may not be available when calling the draw objects method.

How is this even achieved? Keep a link to the graphics context or view that the drawing requested and draw it there is delayed when the internal representation of the objects is fully displayed?

Or are there other standard cocoa mechanisms for handling this (for example, NSImagedoing a lazy drawing when initializing with NSURL)?

Explanations:


  • I am on MacOS, not iOS
  • Using some is NSView -setNeedsDisplay:not the answer I'm looking for ( NSImagedoesn't rely on -setNeedsDisplay:)
+5
source share
2 answers

You must learn NSOperation ; either NSInvocationOperationif you have a specific rendering object, or NSBlockOperationif the rendering is simple enough to fit into a single function.

If you can start rendering before moving on to your view drawRect:, do it (maybe your application delegate will start the process right away when it starts). Otherwise, check drawRect:if the content is available; if not, start the operation and continue working with another drawing. When the rendering object completes its work, it will probably either post a notification, or if you return a link to the view, callsetNeedsDisplay:

, . , ( n n ), NSImage , ( ), .

: NSImage "" setNeedsDisplay: , . , , ; , "" - . initByReferencingURL:, URL-, , (, , ) , , , initWithURL:, . ; , , .

NSImage ; , , , , "renderer", NSImage.

MORE:

drawRect:

- (void)drawRect:(NSRect)dirtyRect {
    NSLog(@"Entered: %@", NSStringFromSelector(_cmd));
    // Use a nice big image of the Milky Way -- this is about 5MB
    NSImage * lazyImage = [[[NSImage alloc] initByReferencingURL:
                            [NSURL URLWithString:@"http://www.eso.org/public/archives/images/original/milkyway.jpg"]]
                           autorelease];
    NSLog(@"Image instantiated.");
    [lazyImage drawInRect:[self bounds] fromRect:NSZeroRect operation:NSCompositeCopy fraction:1.0];
    NSLog(@"Image drawn");  // 2 minutes later; sometimes 3 in my testing
    [[NSColor yellowColor] set];
    [[NSBezierPath bezierPathWithRect:NSInsetRect([self bounds], 4, 4)] stroke];
    NSLog(@"Bezier path drawn; exiting drawRect.");
}

; , , , , ( ):

2011-04-27 21:33:00.899 SetNeedsDisplay[80162:a0b] Entered: drawRect:
2011-04-27 21:33:00.901 SetNeedsDisplay[80162:a0b] Image instantiated.
2011-04-27 21:34:57.911 SetNeedsDisplay[80162:a0b] Image drawn.
2011-04-27 21:34:57.912 SetNeedsDisplay[80162:a0b] Bezier path drawn; exiting drawRect.
+1

All Articles