【问题标题】:Retrieving UIImage from UIImageView in Swift在 Swift 中从 UIImageView 中检索 UIImage
【发布时间】:2016-03-26 02:05:23
【问题描述】:

我找到了解决方案,但它不在 Swift 中:Converting UIImageView to UIImage

我什至通过使用 UnsafeMutablePointed 尝试在 Swift 中实现它,但没有运气。

然而,指针在 Swift 中通常不受欢迎,如果有一个简单的单行解决方案就更好了!

示例代码:

let test1:UIImageView = UIImageView(image: UIImage(named: "test")) 
let test2 = test1.image // This fails with an error: Instance member ‘test1’ cannot be used on type ‘viewController’

【问题讨论】:

    标签: ios swift pointers uiimageview uiimage


    【解决方案1】:

    我强烈建议您学习 Objective-C,因为这将极大地帮助您了解如何在 iOS (Cocoa-Touch) 中做事。

    解决方案与Objective-C相同:

    imageView.image
    

    【讨论】:

    • 不起作用。 let test1:UIImageView = UIImageView(image: UIImage(named: "test.jpg")) let test2 = test1.image
    • @theflarenet 确保您在函数中使用它,因为看起来错误与不在函数中有关。如果您使用的是游乐场,请确保它不只是坐在没有任何函数包装的类/结构中。
    【解决方案2】:

    UIImageView 有一个名为image 的属性,该属性存储其UIImage。因此,要检索UIImageView 的图像,您需要做的就是访问该属性:

    let myImage = imageView.image
    

    这就是它的全部内容。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多