Retrieving UIImage from UIImageView in Swift

陌路散爱 提交于 2019-12-20 02:26:11

问题


I've found the solution but it is not in Swift: Converting UIImageView to UIImage

I've even went rogue by trying to implement it in Swift by using UnsafeMutablePointed and had no luck.

However, pointers are typically frowned upon in Swift and it would be nice if there was a simple one-liner solution to this!

Example Code:

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’

回答1:


I highly advise you to learn about Objective-C, as that will help you greatly in finding out how to do things in iOS (Cocoa-Touch).

The solution is identical to Objective-C:

imageView.image



回答2:


The UIImageView has a property called image which stores its UIImage. So to retrieve an UIImageView's image, all you need to do is access the property:

let myImage = imageView.image

That's really all there is to it.



来源:https://stackoverflow.com/questions/36230826/retrieving-uiimage-from-uiimageview-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!