UIImageView中有一个UIScrollView可供用户缩放并保存:
swift - 当获取UIScrollView的可见内容时,多余的空格-LMLPHP
导航栏下和选项卡栏顶部的区域是UIScrollView框架。
当用户点击完成后,图像将保存在相机卷中。它保存在那里(照片应用程序):
swift - 当获取UIScrollView的可见内容时,多余的空格-LMLPHP
我不知道保存的图像中的空白是什么。
这是我保存图像的代码:

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的可见区域。

最佳答案

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

关于swift - 当获取UIScrollView的可见内容时,多余的空格,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37243733/

10-14 23:20