• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 如何将 UIScrollView 的变换应用到 UIView?

[复制链接]
菜鸟教程小白 发表于 2022-12-12 18:24:30 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

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

paintView.transform = allView.transform

并将其保存到磁盘。

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

View Controller

- (void)scrollViewDidScrollUIScrollView *)scrollView;                                               {
    self.paintView.transform =self.allView.transform;
    [self.paintView setNeedsDisplay];   
}
  - (void)scrollViewDidZoomUIScrollView *)scrollView{
         self.backgroundView.transform = self.allView.transform;
        [self.paintView setNeedsDisplay];
    }

绘画 View

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

- (void)drawRectCGRect)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



Best Answer-推荐答案


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

// 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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap