开发者

CALayer shadows offset too much with renderInContext to a smaller context

I have a UIView, with UIViews as subviews. Each of these sub-UIViews draws an image using drawRect, and may have a shadow using CALayer shadowOffset, shadowRadius, shadowOpacity.

This all works great, but I take a thumbnail picture of the top UIView using renderInContext. The shadows all show up at great distance from the sub-UIView.

This would be correct, because of the shadow offset, but I need the shadow offset to be less (or zero) during the renderInContext method, because the thumbnail context is a lot smaller than the UIView context.

I could renderInContext to a large context and then scale, but Instruments shows the large context memory usage jumps 3-4mb for that short time, and memory is tight.

Is there another way of achieving this? Or a speedy alternative to the renderInContext method for taking a thumbnail capture of a UIView hierarchy?


Edit: Instead of using the CALayer shadow开发者_运维技巧Offset, I have tried using CGContextSetShadowWithColor inside drawRect: - this does work, but the shadows are being clipped, even though clipsToBounds is set to NO. Any ideas on this alternative?

Thanks.


This answer fixes it: Getting a resized screenshot from a UIView

I had been using:

 thumbnailSize = CGSizeMake(PAGE_THUMB_HEIGHT, PAGE_THUMB_WIDTH);
 UIGraphicsBeginImageContext(thumbnailSize);
 CGContextScaleCTM(UIGraphicsGetCurrentContext(), thumbnailSize.width/viewToCapture.bounds.size.width, thumbnailSize.height/viewToCapture.bounds.size.height);
 [viewToCapture.layer renderInContext:UIGraphicsGetCurrentContext()];
 UIImage *flattenedImage = UIGraphicsGetImageFromCurrentImageContext();
 UIGraphicsEndImageContext();

but the UIGraphicsBeginImageContextWithOptions fixes it:

CGSize thumbnailSize = CGSizeMake(PAGE_THUMB_WIDTH, PAGE_THUMB_HEIGHT);
CGFloat scaleX =  thumbnailSize.width / viewToCapture.bounds.size.width;
CGFloat scaleY = thumbnailSize.height / viewToCapture.bounds.size.height;
UIGraphicsBeginImageContextWithOptions(viewToCapture.bounds.size, YES, scaleX > scaleY ? scaleY : scaleX);
[viewToCapture.layer renderInContext:UIGraphicsGetCurrentContext()];
 UIImage *flattenedImage = UIGraphicsGetImageFromCurrentImageContext();
 UIGraphicsEndImageContext();
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜