简体   繁体   中英

How do I store images into UIImage array (Swift)

Here is my code

self.pages?.photo1.getDataInBackgroundWithBlock {
                (imageData: NSData?, error:NSError?) -> Void in
                if error == nil {
                    let image = UIImage(data: imageData!)
                    println(image!)
                    self.pageImages.append(image!)

when I run a println(pageImages.count) I get a 0 even though I know there are other elements in my dataset (pages)

NOTE: "photo1" is stored in a dictionary as a PFFIle type

and a println(image) displays the following result

UIImage: 0x7f90f3e12df0, {748, 420}}

The problem is that you cannot add an element to an array the way you did.

To add an element at the end, use

pageItems.append(image)

To insert an element at the beginning, use

pageItems.insert(image, atIndex: 0)

The following works as expected in Playground:

var imageArray: [UIImage] = []
let image = UIImage()
imageArray.append(image)
imageArray.insert(image, atIndex: 0)
let x = imageArray.count               // 2

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