0

I have an UIImage array, images, which is declared as

var images = [UIImage]()

I currently have pictures stored in there, and when I output it using the println() function, I get

[<UIImage: 0x195dc810>, <UIImage: 0x195da3e0>, <UIImage: 0x1950ab50>]

I was wondering how I could upload these images to Parse as one row.

Here is my full code of what I have now(not working):

@IBAction func saveToParse(sender: AnyObject) {

    for var i = 0; i < images.count; i++
    {
        var objectForSave:PFObject = PFObject(className: "NewLog")
        //error on this line below:  [(UIImage)] does not have a member variable named 'objectAtIndex'
        let imageData:NSData = NSData(data: UIImagePNGRepresentation(images.objectAtIndex(i) as! UIImage))

        var imageFile:PFFile = PFFile(data: imageData)
        imageFile.saveInBackgroundWithBlock({ (success:Bool, error:NSError?) -> Void in
            if success{
                objectForSave.setObject(imageFile, forKey: "Image")

                objectForSave.saveInBackgroundWithBlock({ (success:Bool, error:NSError?) -> Void in
                    if success{
                        //do smth
                    }else{
                        println(error)
                    }
                })

            }else{
                println(error)
            }

            }, progressBlock: { (progress:Int32) -> Void in

        })


    }     
    } 

Thank you.

3
  • 1
    try for image in images this way you will one image at a time, you can use this instead of the images.objectAtIndex(i) Commented Jun 27, 2015 at 11:18
  • Hey milo526, THANK YOU, worked wonderfully. I am able to upload my picture to parse. Commented Jun 27, 2015 at 17:17
  • @JoshO'Connor Hey Josh, I have trying to do this feature (upload multiple images to parse) my self for over a week now but had absolute no luck! I only just came across your post! I have just tried to use the code above, and also changed the error for milo's answer. But it still doesn't seem to work!( it still gives me an error for image in images) I'll open a question if you would be kind enough to help me figure this out! ;) Best regards Commented Feb 25, 2016 at 11:11

1 Answer 1

1

Instead of iterating over the array and using the index, you can iterate over all the items in the array.

for image in images

This will get every single image in the array. Everything in the for loop will be executed once for every item in the array.

You would use image instead of images.objectAtIndex(i) as! UIImage

This will also make your code easier to read for others ;)

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.