ios - 如何将 UIScrollView 的转换应用于 UIView?

标签 ios uiscrollview core-graphics drawrect

在我的应用程序中,我有一个 ScrollView ,其中添加了一个名为 allView 的 subview 。 在 scrollview 委托(delegate)方法中,我将 scrollview subview 的当前转换值应用到另一个名为 paint view 的 View

paintView.transform = allView.transform

并将其保存到磁盘。

在该过程中创建的图像看起来与屏幕上的不同。为什么?我该如何解决?

View Controller

- (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];
    }

绘制 View

在 PaintView 的绘制矩形内,我试图从 ScrollView 应用转换

- (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 模拟器 Desired Effect

图像保存到磁盘 Result

最佳答案

在没有运行代码的情况下诊断这里的问题可能有点困难。但是,我认为问题可能出在您剪裁图像的这些行上:

// Clipping the image
CGImageRef cgImg = CGImageCreateWithImageInRect(image.CGImage, rect);
UIImage *img = [UIImage imageWithCGImage:cgImg];

我认为您实际上只想获取 ScrollView 的可见矩形,即 scrollView.frame 而不是填充 ScrollView 整个 contentSize 的 subview 。因此,使用某种方式(例如 paintView 上的属性,例如 visibleFrame),我会将这些行修改为如下所示:

// Clipping the image
CGImageRef cgImg = CGImageCreateWithImageInRect(image.CGImage, self.visibleFrame);
UIImage *img = [UIImage imageWithCGImage:cgImg];

希望这可以帮助您解决这个问题!

关于ios - 如何将 UIScrollView 的转换应用于 UIView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21448138/

相关文章:

javascript - 苹果如何定义 "user action"?

ios - 从 Facebook 单击的 Ionic Cordova Branch 深层链接返回 `+clicked_branch_link: false`

swift - UIScrollView 宽度比屏幕宽

xaml - Xamarin 表单 : How to fix Activity Indicator in Centre of Screen in Scrollview

ios - 如何平滑绘制图像的边缘?

iphone - CoreData : error: Serious application error. 核心数据更改处理期间捕获到异常

ios - 如何设置 UIImage 的不透明度/alpha?

ios - 水平或垂直滚动​​,但不能同时滚动

ios - xCode - 帮助尝试将撤消/重做功能添加到 UIBezierPath

swift - 中心 CAShapeLayer