【发布时间】:2017-12-09 10:50:39
【问题描述】:
我在捕获自定义视图时遇到问题。我正在尝试设置大小为UIImage 图像数据大小的捕获图像,但是当我截取屏幕截图时,图像是这样的! :
图像缩小! 这是代码:
UIGraphicsBeginImageContextWithOptions((self.photoImage.size), false, UIScreen.main.scale)
self.captureView.layer.render(in: UIGraphicsGetCurrentContext()!)
self.photoToShare = UIGraphicsGetImageFromCurrentImageContext()
print(self.photoToShare.size) //the size is right
UIGraphicsEndImageContext()
self.photo.image = self.photoToShare!
什么问题?!
我累了:
self.captureView.drawHierarchy(in: self.captureView.bounds , afterScreenUpdates: true)
还是没有成功
编辑
我尝试了这个方法,捕获然后裁剪它,但仍然没有成功
UIGraphicsBeginImageContextWithOptions(self.captureView.bounds.size, false, UIScreen.main.scale)
self.captureView.drawHierarchy(in: self.captureView.bounds, afterScreenUpdates: true)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
let cropRect = CGRect(x: 0, y: 0, width: (self.photoImage.size.width) , height: (self.photoImage.size.height))
UIGraphicsBeginImageContextWithOptions(cropRect.size, false, UIScreen.main.scale)
image?.draw(in: cropRect)
let finalImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
self.photo.image = finalImage
但图像会失真:
【问题讨论】:
-
尝试在
UIGraphicsBeginContextWithOptions之前调用[self.captureView setNeedsLayout]; [self.captureView layoutIfNeeded]。另外,如果这不起作用,请尝试使用self.captureView.bounds,而不是将大小设置为photoImage.size。 -
谢谢我没有工作!实际上我需要照片数据大小。
-
您是否尝试过像stackoverflow.com/questions/11318020/… 那样缩放捕获的图像?
-
@Ellen 是的,但实际上我需要用准确的 UIimage 的数据大小宽度和高度来捕获 UIIView
-
只是为了确定在 ImageView 中显示图像时您使用的是 AspectFit 比例还是比例填充?
标签: ios iphone swift xcode uiimage