[英]How can I save only the image in an image view?
I am working on an iOS App. 我正在使用iOS应用程序。 I'm pretty new to iOS dev.
我是iOS开发人员的新手。 I'm using an image view in this app.
我正在此应用程序中使用图像视图。 The size of the image will vary - it is not static.
图像的大小会有所不同-它不是静态的。 It is centered in the view, and will always be the width of the screen.
它在视图中居中,并且始终是屏幕的宽度。 The height will vary.
高度会有所不同。 I am overlaying text overtop of the image.
我将文本覆盖在图像上方。
My current code captures the entire screenshot, minus the navigation and toolbars. 我当前的代码捕获了整个屏幕截图,减去了导航和工具栏。 How can I modify my existing save image function to only capture the image being displayed rather than the full screen?
如何修改现有的保存图像功能以仅捕获正在显示的图像而不是全屏图像?
func generateImage() -> UIImage
{
// hide toolbar and navbar
toolBar.hidden = true
navigationController?.setNavigationBarHidden(true, animated: false)
// Render view to an image
UIGraphicsBeginImageContext(self.view.frame.size)
view.drawViewHierarchyInRect(self.view.frame,
afterScreenUpdates: true)
let img : UIImage =
UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
// show toolbar and navbar
toolBar.hidden = false
navigationController?.setNavigationBarHidden(false, animated: false)
return img
}
You mention that you are using a UIImageView in order to display the image. 您提到您正在使用UIImageView来显示图像。 Then you have shown code that is taking a screenshot to get access to a UIImage object.
然后,您显示了正在截取屏幕截图以访问UIImage对象的代码。
I may be very confused here, but the UIImageView class has a property called image
that will give you the UIImage currently being displayed. 我在这里可能会很困惑,但是UIImageView类具有一个名为
image
的属性,它将为您提供当前正在显示的UIImage。 Why not just grab this and use it wherever you need it? 为什么不抓住它并在需要的地方使用它呢?
let myImage = myImageView.image
Replace myImageView
with a reference to your actual UIImageView
instance inside your UIViewController
. 将
myImageView
替换为对UIViewController
实际UIImageView
实例的引用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.