gpt4 book ai didi

ios - 如何只保存 ImageView 中的图像?

转载 作者:搜寻专家 更新时间:2023-11-01 05:38:36 25 4
gpt4 key购买 nike

我正在开发 iOS 应用程序。我是 iOS 开发人员的新手。我在此应用程序中使用 ImageView 。图像的大小会有所不同 - 它不是静态的。它在 View 中居中,并且始终是屏幕的宽度。高度会有所不同。我在图像上叠加了文字。

我当前的代码捕获整个屏幕截图,不包括导航和工具栏。如何修改我现有的保存图像功能以仅捕获正在显示的图像而不是全屏?

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
}

最佳答案

您提到您正在使用 UIImageView 来显示图像。然后您展示了截取屏幕截图以访问 UIImage 对象的代码。

我可能在这里很困惑,但是 UIImageView 类有一个名为 image 的属性,它会为您提供当前正在显示的 UIImage。为什么不捕获它并在任何需要的地方使用它呢?

let myImage = myImageView.image

myImageView 替换为对 UIViewController 中的实际 UIImageView 实例的引用。

UIImageView Documentation

关于ios - 如何只保存 ImageView 中的图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33771139/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com