Ios 如何应用UIScrollView';转换到UIView?
在我的应用程序中,我有一个scrollview,其中添加了一个名为allView的子视图。 在scrollview委托方法中,我将scrollview子视图的当前转换值应用于另一个名为paint view的视图Ios 如何应用UIScrollView';转换到UIView?,ios,uiscrollview,core-graphics,drawrect,Ios,Uiscrollview,Core Graphics,Drawrect,在我的应用程序中,我有一个scrollview,其中添加了一个名为allView的子视图。 在scrollview委托方法中,我将scrollview子视图的当前转换值应用于另一个名为paint view的视图 paintView.transform = allView.transform 并将其保存到磁盘 该过程产生的图像与屏幕上的图像看起来不同。为什么?我怎样才能修好它 视图控制器 - (void)scrollViewDidScroll:(UIScrollView *)scrollView
paintView.transform = allView.transform
并将其保存到磁盘
该过程产生的图像与屏幕上的图像看起来不同。为什么?我怎样才能修好它
视图控制器
- (void)scrollViewDidScroll:(UIScrollView *)scrollView; {
self.paintView.transform =self.allView.transform;
[self.paintView setNeedsDisplay];
}
- (void)scrollViewDidZoom:(UIScrollView *)scrollView{
self.backgroundView.transform = self.allView.transform;
[self.paintView setNeedsDisplay];
}
绘制视图
在PaintView的draw rect中,我试图应用滚动视图和
- (void)drawRect:(CGRect)rect
{
// Drawing code
// Draw on the screen
CGContextRef ctx1 =UIGraphicsGetCurrentContext();
CGContextConcatCTM(ctx1, self.transform);
CGColorRef wh = [[UIColor redColor]CGColor];
CGContextSetStrokeColorWithColor(ctx1, wh);
CGContextMoveToPoint(ctx1, 0, 0);
CGContextAddLineToPoint(ctx1, 200, 200);
CGContextStrokePath(ctx1);
// Apply scroll view's transformation
CGRect r = CGRectApplyAffineTransform(rect,self.transform );
//that gives a resized image
UIGraphicsBeginImageContextWithOptions(r.size, NO, 0.0);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextConcatCTM(ctx, self.transform);
// stroke and so on
CGContextSetStrokeColorWithColor(ctx, wh);
CGContextMoveToPoint(ctx, 0, 0);
CGContextAddLineToPoint(ctx, 200, 200);
CGContextStrokePath(ctx);
Getting image with entire content.
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
//Clipping the image
CGImageRef cgImg = CGImageCreateWithImageInRect(image.CGImage, rect);
UIImage *img = [UIImage imageWithCGImage:cgImg];
NSData * d = UIImageJPEGRepresentation(img, 0.8);
//saving the image (for debugging)
[self save:d];
UIGraphicsEndImageContext();
}
iOS模拟器
保存到磁盘的图像
如果没有运行的代码,在这里诊断问题可能有点困难。但是,我认为问题可能出在以下几行,即您正在剪切图像:
// Clipping the image
CGImageRef cgImg = CGImageCreateWithImageInRect(image.CGImage, rect);
UIImage *img = [UIImage imageWithCGImage:cgImg];
我认为您实际上只需要获取滚动视图的可见矩形,即scrollView.frame
,而不是填充滚动视图整个contentSize
的子视图。因此,使用某种方法(例如paintView
上的属性,例如visibleFrame
),我会将这些行修改为如下所示:
// Clipping the image
CGImageRef cgImg = CGImageCreateWithImageInRect(image.CGImage, self.visibleFrame);
UIImage *img = [UIImage imageWithCGImage:cgImg];
希望这有助于你通过这个问题 这种情况是否只发生在视网膜屏幕上?