代码之家  ›  专栏  ›  技术社区  ›  Maysam

获取UIScrollView的可见内容时出现额外的空白

  •  1
  • Maysam  · 技术社区  · 8 年前

    有一个 UIImageView 在一个 UIScrollView 哪个用户可以将其向下和向上缩放,然后保存:

    enter image description here

    下面的区域 导航栏 和顶部 标签栏 UI滚动视图 框架

    当用户点击时 多恩 ,图像将保存在相机卷中。它保存在那里(照片应用程序): enter image description here

    我不知道保存的图像中的空白是什么。

    我的代码是保存图像:

    UIGraphicsBeginImageContextWithOptions(scrollView.frame.size, false, 0.0)
                let rect = CGRectMake(0, scrollView.frame.origin.y, scrollView.frame.size.width, scrollView.frame.size.height)
                self.view.drawViewHierarchyInRect(rect, afterScreenUpdates: true)
                let image = UIGraphicsGetImageFromCurrentImageContext()
                let imageData = UIImageJPEGRepresentation(image, 1)
                let compressedJPGImage = UIImage(data: imageData!)
                UIImageWriteToSavedPhotosAlbum(compressedJPGImage!, nil, nil, nil)
    

    我要保存的是 UI滚动视图 .

    1 回复  |  直到 8 年前
        1
  •  0
  •   Maysam    8 年前

    我不得不使用 CGContextTranslateCTM() 要平移矩形,请执行以下操作:

        let screenRect: CGRect = scrollView.bounds
        UIGraphicsBeginImageContext(screenRect.size)
        let ctx: CGContextRef = UIGraphicsGetCurrentContext()!
        CGContextTranslateCTM(ctx,0,-scrollView.frame.origin.y)
        UIColor.blackColor().set()
        CGContextFillRect(ctx, screenRect)
            view.layer.renderInContext(ctx)
        let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()