开发者

Add background to IKImageView

开发者 https://www.devze.com 2023-03-14 00:02 出处:网络
I want to add a checkerboard background to an IKImageView for viewing transparent images. I am trying to set CALayer with checkboard image to IKImageView with message setOverlay:forType

I want to add a checkerboard background to an IKImageView for viewing transparent images. I am trying to set CALayer with checkboard image to IKImageView with message setOverlay:forType

CFURLRef imageURL = CFBundleCopyResourceURL(CFBundleGetMainBundle(),(CFStringRef)@"bgImage.png", NULL, NULL);
CGImageSourceRef imageSource = CGImageSourceCreateWithURL(imageURL,NULL);
CFRelease(imageURL);
CGImageRef image = CGImageSourceCreateImageAtIndex(imageSource, 0,NULL);
CFRelease(imageSource);
CALayer *bgLayer = [[CALayer alloc] init ];
[bgLayer setContents:image ];
[ imageView setOverlay:bgLayer forType:IKOverlayTypeBackground ];

but doesn't work. I get this message in console

could not add '<CALayer: 0x101d039a0>开发者_如何学JAVA;' linkedTo 'kIKRootLayerType'

Does anyone knows what is wrong or any workarround ?

Thanks


The error message is because the view is not attached to top view.

Finally I added the checkerboard background to the NSScrollview container of the IKImageview, this way the background layer auto resizes

CALayer *layer=[[SquareBackgroundLayer alloc] init];
[[ scrollView contentView] setLayer:layer];
[ layer release ];

I hope this is helpful for anyone.

0

精彩评论

暂无评论...
验证码 换一张
取 消