2

In my app I use IKImageBrowserView with background. If wantsLayer NO - all fine and IKImageBrowserView look like nice. But if I enabled wantsLayer (in parent view) the background in IKImageBrowserView is corrupt. (Sorry English is not my native language and I can't find the correct word).

If I understand correctly, problem in this fragment. But I can't see where.

NSRect visibleRect = [owner visibleRect];
NSRect bounds = [owner bounds];

CGImageRef image = NULL;
NSString *path = [[NSBundle mainBundle] pathForResource:[@"metal_background.tif" stringByDeletingPathExtension] ofType:[@"metal_background.tif" pathExtension]];
if (!path) {
    return;
}

CGImageSourceRef imageSource = CGImageSourceCreateWithURL((__bridge CFURLRef)[NSURL fileURLWithPath:path], NULL);
if (!imageSource) {
    return;
}

image = CGImageSourceCreateImageAtIndex(imageSource, 0, NULL);
if (!image) {
    CFRelease(imageSource);
    return;
}

float width = (float) CGImageGetWidth(image);
float height = (float) CGImageGetHeight(image);

//compute coordinates to fill the view
float left, top, right, bottom;

top = bounds.size.height - NSMaxY(visibleRect);
top = fmod(top, height);
top = height - top;

right = NSMaxX(visibleRect);
bottom = -height;

// tile the image and take in account the offset to 'emulate' a scrolling background
for (top = visibleRect.size.height-top; top>bottom; top -= height){
    for(left=0; left<right; left+=width){
        CGContextDrawImage(context, CGRectMake(left, top, width, height), image);
    }
}

CFRelease(imageSource);
CFRelease(image);

Image with problem

Image without problem

Thanks

2 Answers2

0

I tried using IKImageView a few weeks ago and ran in to a number if problems. It's a nice class but it doesn't seem to have been updated in a long time. I doesn't support retina screen and, as you point out goes crazy when you add a layer.

I wanted to do some custom drawing with CALayer over the top of an image. The first thing I tried was to add a layer to the image view, which gave me problems. The solution I went for want to add a custom NSView subview to the image view, add auto layout constraints so that it always the same size, then add a layer hosting view to the subview. This worked fine, so move your drawing code to a custom subview and it should work.

IKImageView
    LayerHostingView (<--- add CALayer here)
Daniel Farrell
  • 9,316
  • 8
  • 39
  • 62
0

I ran in to this distorted background image problem as well and it turned out to be that I had set up the CALayer for the background incorrectly. I suspect that you are misunderstanding the cause to be setting wantsLayer to false because when that is set the layer does not immediately redraw new layers that you set.

I reached this conclusion as if I set up the layer correctly, then set wantsLayer false immediately before setting a bad CALayer in the next line of code I see the correct, undistorted image first and only when I scroll (and it redraws) do I see the buggy effects in your screenshot. However if I set wantsLayer true at the same point in code I see the buggy graphics immediately when the browser loads, without having to redraw first.

You can move the wantsLayer false statement further down your code to find the line that's breaking it, it'll be where the layer is reset/updated somewhere.

Jay
  • 902
  • 2
  • 12
  • 27