2011-08-26 50 views
0

我有一个UIView,与UIViews作为子视图。每个这些子UIViews的绘制使用的drawRect的图像,并且可以具有使用的CALayer shadowOffset,shadowRadius,shadowOpacity阴影。CALayer的阴影偏移太多与renderInContext到一个较小的范围内

这一切工作很大,但我采取使用renderInContext顶部的UIView的缩略图。阴影都显示在离子UIView很远的地方。

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

我可以renderInContext一个大背景,然后规模,但仪器显示的大背景下的内存使用跳跃3-4MB的这短短的时间,和内存资源紧张。

有没有实现这一点的另一种方式?或者是renderInContext方法的快速替代方法,用于获取UIView层次结构的缩略图捕获?


编辑: 而不是使用的CALayer shadowOffset的,我一直在使用CGContextSetShadowWithColor内的drawRect尝试: - 这个工作,但阴影被修剪,即使clipsToBounds设置为NO。关于这个选择的任何想法?

谢谢。

回答

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