Pregunta

i have a two-paned NSSplitView, one subview of it is an IKImageBrowserView. i need to capture a bitmap of it to swap into place before animating the NSSplitView with a CATransition.

using cacheDisplayInRect does not capture the IKImageBrowserView contents, but does capture the other pane's contents fine. i'm thinking that this is likely do to with IKImageBrowserView having its own layer-tree.

can someone confirm that this is true ?

NSBitmapImageRep *imageRep = [self.imagesSplitView bitmapImageRepForCachingDisplayInRect:visibleRect];
        [self.imagesSplitView cacheDisplayInRect:visibleRect toBitmapImageRep:rep];
        NSImage *tempImage = [[NSImage alloc] initWithCGImage:[imageRep CGImage] size:visibleRect.size];
        [self.tempView setImage:tempImage];
        [self.holdingView replaceSubview:self.imagesSplitView with:self.tempView];

are there any suggestion about how i can get bitmap copy of the view contents ?

renderInContext is not going to help me, i don't think, because IKImageBrowserView doesn't reveal methods for referencing its presentationLayer -- if it even has one.

i think ImageKit is a combination of CALayers/CAOpenGLLayers and alchemy.

thanks.

¿Fue útil?

Solución

without getting any confirmations here, and not being able to use cacheDisplayInRect method; i've found that using Quartz Window Services API works.

at seems that using replaceSubView needs a delay, so i did that with a block and was successful at capturing the bitmap.

here's the code that works for me in case this helps someone else:

NSScrollView *sc = [self.browserView enclosingScrollView]; // i need the scrollView in the image
NSRect viewRect = [sc visibleRect];
NSRect transRect = [[self.window contentView] convertRect:viewRect fromView:sc];

CGWindowID windowID = (CGWindowID)[self.window windowNumber];
CGWindowListOption singleWindowOptions = kCGWindowListOptionIncludingWindow;
CGImageRef windowImage = CGWindowListCreateImage( CGRectNull, singleWindowOptions, windowID, kCGWindowImageBoundsIgnoreFraming);
CGImageRef viewImage = CGImageCreateWithImageInRect(windowImage, NSRectToCGRect(transRect));
NSBitmapImageRep *bitMapRep = [[NSBitmapImageRep alloc] initWithCGImage:viewImage];
NSImage *image = [[NSImage alloc] init];
[image addRepresentation:bitMapRep];
NSImageView *iv = [[NSImageView alloc] initWithFrame:transRect]; 
[iv setImage: image];

double delayInSeconds = 0.01;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void) {

    [[self.window contentView] replaceSubview:sc with:iv];
    });
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top