【问题标题】:CALayer shadows offset too much with renderInContext to a smaller contextCALayer 阴影使用 renderInContext 偏移太多到较小的上下文
【发布时间】:2011-11-04 08:25:13
【问题描述】:

我有一个 UIView,其中 UIViews 作为子视图。这些子 UIView 中的每一个都使用 drawRect 绘制图像,并且可能使用 CALayer shadowOffset、shadowRadius、shadowOpacity 产生阴影。

这一切都很好,但我使用 renderInContext 拍摄了顶部 UIView 的缩略图。阴影都显示在距离子 UIView 很远的地方。

这是正确的,因为阴影偏移,但我需要在 renderInContext 方法期间阴影偏移更小(或为零),因为缩略图上下文比 UIView 上下文小很多。

我可以将 InContext 渲染到大型上下文然后进行缩放,但 Instruments 显示大型上下文内存使用量在短时间内跃升了 3-4mb,而且内存很紧张。

还有其他方法可以实现吗?或者快速替代 renderInContext 方法来获取 UIView 层次结构的缩略图?


编辑: 我没有使用 CALayer shadowOffset,而是尝试在 drawRect 中使用 CGContextSetShadowWithColor: - 这确实有效,但是阴影被剪裁了,即使 clipsToBounds 设置为 NO。关于这个替代方案的任何想法?

谢谢。

【问题讨论】:

    标签: iphone calayer


    【解决方案1】:

    这个答案解决了它: Getting a resized screenshot from a UIView

    我一直在使用:

     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();
    

    但 UIGraphicsBeginImageContextWithOptions 修复了它:

    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();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-28
      • 2012-04-17
      • 2020-12-15
      • 1970-01-01
      • 1970-01-01
      • 2018-07-04
      相关资源
      最近更新 更多