将大型UIView调整为内容视图



我已经在故事板的视图中插入了一个UIView,我需要使用方法在1000px x 1000px处拍摄一张图像

UIGraphicsBeginImageContext(createBoard.layer.frame.size)
createBoard.layer.renderInContext(UIGraphicsGetCurrentContext())
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
//Save it to the camera roll
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)

然而,我认为swift正在重新调整视图的大小,以显示在我的显示器上,因为当我以1000像素拍摄图像时,它会像素化。我的问题是,我是否可以制作一个1000px x 1000px的UIView,并将其缩小以显示给用户,但当拍摄图像时,它会以原始的1000px进行?

问题可能是您没有考虑不同的比例因素(视网膜/非视网膜)。

您可以尝试使用:

UIGraphicsBeginImageContextWithOptions(_ size: CGSize, _ opaque: Bool, _ scale: CGFloat)

并将0传递给刻度(它根据设备的屏幕自然比例因子自动设置刻度)

最后,如果你的目标是iOS7+,你也可以使用一个更新的视图快照api:

-drawViewHierarchyInRect:afterScreenUpdates:

如本文所述

最新更新