Ios 基于图像大小的截屏
我无法捕获自定义视图。我试图设置捕获图像的大小Ios 基于图像大小的截屏,ios,iphone,swift,xcode,uiimage,Ios,Iphone,Swift,Xcode,Uiimage,我无法捕获自定义视图。我试图设置捕获图像的大小UIImageimage数据的大小,但是当我拍摄屏幕截图时,图像是这样的!: 图像缩小了! 代码如下: UIGraphicsBeginImageContextWithOptions((self.photoImage.size), false, UIScreen.main.scale) self.captureView.layer.render(in: UIGraphicsGetCurrentContext()!)
UIImage
image数据的大小,但是当我拍摄屏幕截图时,图像是这样的!:
图像缩小了!
代码如下:
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,AfterScreenUpdate: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
编辑
我尝试了这种方法,捕获并裁剪了它,但仍然没有成功
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
但图像将被扭曲:
我尝试使用aspectToFit内容模式捕获imageview的屏幕截图&使用下面的方法没有恒定的宽度和高度
func captureShot() {
let layer : CALayer = self.imageView!.layer
UIGraphicsBeginImageContextWithOptions((layer.frame.size), false, 1.0);
layer.render(in: UIGraphicsGetCurrentContext()!)
let screenshot = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
}
&返回的屏幕截图具有与图像原始大小相同的图像大小(229*220)
我尝试使用aspectToFit内容模式捕获imageview的屏幕截图&使用下面的方法没有恒定的宽度和高度
func captureShot() {
let layer : CALayer = self.imageView!.layer
UIGraphicsBeginImageContextWithOptions((layer.frame.size), false, 1.0);
layer.render(in: UIGraphicsGetCurrentContext()!)
let screenshot = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
}
&返回的屏幕截图具有与图像原始大小相同的图像大小(229*220)
试试这个:
extension UIView {
var screenCapture: UIImage {
UIGraphicsBeginImageContextWithOptions(bounds.size, opaque, 0.0)
drawViewHierarchyInRect(bounds, afterScreenUpdates: true)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
}
要将其置于UImageView中,请执行以下操作:
let imageView = UIImageView()
imageView.contentMode = .scaleAspectFit
imageView.image = someView.screenCapture
imageView.frame = someFrame
someSuperview.addSubview(imageView)
(对所有的一些感到抱歉,但希望不会让人困惑:)
希望这有帮助 试试这个:
extension UIView {
var screenCapture: UIImage {
UIGraphicsBeginImageContextWithOptions(bounds.size, opaque, 0.0)
drawViewHierarchyInRect(bounds, afterScreenUpdates: true)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
}
要将其置于UImageView中,请执行以下操作:
let imageView = UIImageView()
imageView.contentMode = .scaleAspectFit
imageView.image = someView.screenCapture
imageView.frame = someFrame
someSuperview.addSubview(imageView)
(对所有的一些感到抱歉,但希望不会让人困惑:)
希望这有帮助 尝试调用[self.captureView setNeedsLayout];[self.captureView layoutifneed]
就在UIGraphicsBeginContextWithOptions
前面。此外,如果这不起作用,请尝试使用self.captureView.bounds
,而不是将大小设置为photoImage.size
。谢谢,我没有工作!实际上,我需要照片数据大小。您是否尝试过像这样缩放捕获的图像?@Ellen是的,但实际上我需要使用准确的UIimage数据大小宽度和高度捕获UIIView只是为了确保在ImageView中显示图像时,您是否使用AspectFit比率或缩放填充?尝试调用[self.captureView setNeedsLayout];[self.captureView layoutifneed]
就在UIGraphicsBeginContextWithOptions
前面。此外,如果这不起作用,请尝试使用self.captureView.bounds
,而不是将大小设置为photoImage.size
。谢谢,我没有工作!实际上,我需要照片数据大小。您是否尝试过像这样缩放捕获的图像?@Ellen是的,但实际上我需要使用准确的UIimage数据大小宽度和高度捕获UIIView只是为了确保在ImageView中显示图像时,您是否使用AspectFit比率或缩放填充?谢谢!我会尽力感谢你的,它很好用!但我添加了一个裁剪函数,将图像另存为Image.Image size。谢谢!我会尽力感谢你的,它很好用!但是我添加了一个裁剪函数来将图像保存为Image.Image size.@Mc.Lover,为什么大小必须是“Image DATA size”?另外,您是否在设备或模拟器上测试此功能?@Mc.Lover,为什么大小必须为“图像数据大小”?另外,您是否在设备或模拟器上测试此功能?