3
我有一个CATiledLayer支持的UIScrollView,它显示了一个非常大的图像。 我想实现的是将此视图捕获到UIImage中(将其用作进度栏视图的背景)。 有几个问题:iOS - CATiledLayer renderInContext:结果扭曲的图像
- 在“100%”变焦(整个画面可见的)它似乎确定
- ,如果我在滚动视图放大,一些瓷砖成为扭曲(规模和/或偏移是坏的)
- 试图与苹果公司的PhotoScroller样品,几乎工作,但拍摄的图像有时被像素化
- 我没有写代码来摄制,所以我不知道如何解决它
经过对此其他计算器条目,但他们没有太大的帮助...
图像捕捉代码:
CGRect rect = [view bounds];
UIImage* img = nil;
if ([view isKindOfClass:[UIScrollView class]])
{
UIScrollView* scrollview = (UIScrollView*)view;
CGPoint contentOffset = scrollview.contentOffset;
UIGraphicsBeginImageContextWithOptions(rect.size, view.opaque, 0.0);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(ctx, -contentOffset.x, -contentOffset.y);
[view.layer renderInContext:ctx];
img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
而且平铺视图的drawRect的代码:方法:
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
if (self.clipContent)
{
CGContextSaveGState(context);
CGFloat color[4] = {1.0, 1.0f, 1.0f, 1.0f};
CGContextSetFillColor(context, color);
CGContextFillRect(context, self.bounds);
CGRect clips[] = {
CGRectMake(self.bounds.size.width/2.0, 0,
self.bounds.size.height/2.0 , self.bounds.size.width/2.0)
};
CGContextClipToRects(context, clips, sizeof(clips)/sizeof(clips[0]));
}
CGFloat scale = CGContextGetCTM(context).a;
CGSize tileSize = [_dataSource tileSize];
double width = (double)tileSize.width/(double)scale;//pow(2.0, (int)log2(scale));
double height = (double)tileSize.height/(double)scale;//pow(2.0, (int)log2(scale));
int firstCol = floorf(CGRectGetMinX(rect)/width);
int lastCol = floorf((CGRectGetMaxX(rect) - 1)/width);
int firstRow = floorf(CGRectGetMinY(rect)/height);
int lastRow = floorf((CGRectGetMaxY(rect) - 1)/height);
for (int row = firstRow; row <= lastRow; row++) {
for (int col = firstCol; col <= lastCol; col++) {
UIImage* tileImage = [_dataSource tileForScale:scale row:row col:col];
CGRect tileRect = CGRectMake(width * col, height * row, width, height);
tileRect = CGRectIntersection(rect, tileRect);
[tileImage drawInRect:tileRect];
}
}
if (self.clipContent)
CGContextRestoreGState(context);
}
任何想法?
我无法对此表示感谢。在找到这篇文章之前,我几个小时都在渲染不正确的快照。 –
但这样,图像变形!它的形状根本不合适。 @Asylum –