【问题标题】:Extra white space when get visible content of UIScrollView获取 UIScrollView 的可见内容时的额外空白
【发布时间】:2016-05-15 21:11:32
【问题描述】:

UIScrollView 内有一个UIImageView,用户可以将其缩小和放大然后保存:

导航栏下方和标签栏顶部的区域是UIScrollView框架。

当用户点击完成时,图像将保存在相机胶卷中。这是保存在那里的内容(照片应用程序):

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

这是我保存图像的代码:

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)

我要保存的正是UIScrollView的可见区域。

【问题讨论】:

    标签: swift uiscrollview uigraphicscontext


    【解决方案1】:

    我不得不使用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()
    

    【讨论】:

      猜你喜欢
      • 2010-10-26
      • 2020-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-07
      相关资源
      最近更新 更多