0

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)
1
  • 2
    imageArray.map { UIImagePNGRepresentation($0) as AnyObject } Commented Apr 2, 2015 at 0:23

1 Answer 1

2
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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.