简体   繁体   中英

How to covert an array of images to anyobject with swift

I have an array of images and i want to covert them all to an anyObject object. How do i do this all at the same time and not one array index at a time?

Code...

var imageArray:[UIImage] = [Image1, Image2, Image3]

let convertImage:[AnyObject] = UIImagePNGRepresentation(imageArray)
var dataArray:[AnyObject] = [AnyObject]()
for image in imageArray {
    dataArray.append(UIImagePNGRepresentation(image))
}

Or, to steal from @Airspeed Velocity's comment:

let dataArray = imageArray.map { UIImagePNGRepresentation($0) as AnyObject }

This has the nice advantage of letting us declare the data array as a let constant rather than a var .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM