Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift: Creating an array of UIImage

Using Swift, I'm trying to create an array of UIImage objects for a simple animation. Contextual help for animationImages reads, "The array must contain UI Image objects."

I've tried to create said array as follows, but can't seem to get the syntax correct:

var logoImages: UIImage[] logoImages[0] = UIImage(name: "logo.png") 

This throws: ! Variable logoImages used before being initialized

Then I tried

var logoImages = [] logoImages[0] = UIImage(named: "logo.png") 

Which throws: ! Cannot assign to the result of this expression

I've checked the docs here, but the context isn't the same: https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/CollectionTypes.html

like image 291
kmiklas Avatar asked Jun 11 '14 20:06

kmiklas


People also ask

How to make UIImage array in Swift?

If you want to add new objects to an array, you should use Array. append() or some of the equivalent syntactic sugar: logoImages. append(UIImage(named: "logo.

What is UIImage Swift?

An object that manages image data in your app.

How do I change the UIImage size in Swift?

Show activity on this post. extension UIImage { func imageResize (sizeChange:CGSize)-> UIImage{ let hasAlpha = true let scale: CGFloat = 0.0 // Use scale factor of main screen UIGraphicsBeginImageContextWithOptions(sizeChange, ! hasAlpha, scale) self. draw(in: CGRect(origin: CGPoint.


Video Answer


2 Answers

You have two problems (and without a regex!)

1. You aren't creating an array. You need to do:

var logoImages: [UIImage] = [] 

or

var logoImages: Array<UIImage> = [] 

or

var logoImages = [UIImage]() 

or

var logoImages = Array<UIImage>() 

2. If you want to add new objects to an array, you should use Array.append() or some of the equivalent syntactic sugar:

logoImages.append(UIImage(named: "logo.png")!) 

or

logoImages += [UIImage(named: "logo.png")!] 

or

logoImages += [UIImage(named: "logo.png")!, UIImage(named: "logo2.png")!] 

You need to append to the array because (excerpt from docs):

You can’t use subscript syntax to append a new item to the end of an array. If you try to use subscript syntax to retrieve or set a value for an index that is outside of an array’s existing bounds, you will trigger a runtime error. However, you can check that an index is valid before using it, by comparing it to the array’s count property. Except when count is 0 (meaning the array is empty), the largest valid index in an array will always be count - 1, because arrays are indexed from zero.

Of course you could always simplify it when possible:

var logoImage: [UIImage] = [     UIImage(named: "logo1.png")!,     UIImage(named: "logo2.png")! ] 

edit: Note that UIImage now has a "failable" initializer, meaning that it returns an optional. I've updated all the bits of code to reflect this change as well as changes to the array syntax.

like image 62
Jiaaro Avatar answered Sep 20 '22 20:09

Jiaaro


You are declaring the type for logoImages but not creating an instance of that type.

Use var logoImages = UIImage[]() which will create a new array for you.

...and then after creating a new empty Array instance, as described in the answer by @Jiaaro you can't use subscripting to add to an empty array

like image 23
fqdn Avatar answered Sep 19 '22 20:09

fqdn