0

Sorry guys I want to try this again just reword it so its understandable.

ImageFiles is an array of PFObjects, about 10 images

let randomNumber = imageFiles[Int(arc4random_uniform(UInt32(imageFiles.count)))]

    println(randomNumber)

and the println gives me a random image file from the array.

How do I put that into the image view? Meaning i want a random element of the array to be viewable in the uiimage view.

let image = UIImage(data: randomNumber as AnyObject as! NSData)

closest I've got... no syntax error only runtime

Could not cast value of type 'PFObject' (0x105639070) to 'NSData' (0x106c97a48).

enter image description here

enter image description here

dcarney999
  • 51
  • 7

1 Answers1

0

An actual image stored in Parse is not of the format PFObject, it is of the type PFFile. Normally a reference to a PFFile is stored within a PFObject.

I can imagine your code/pseudocode will end up looking like the following:

let myRandomImageMetaData = imageFiles[Int(arc4random_uniform(UInt32(imageFiles.count)))]
let imageFileReference: PFFile = myRandomImageMetaData["imgLink"]

The actual code will depend on your database structure which I've already asked you for here, but you never provided.


Now that you have the file reference, then you actually have to download the image data itself. You can do it like so:

imageFileReference.getDataInBackgroundWithBlock {
    (imageData: NSData?, error: NSError?) -> Void in
    if error == nil {
        if let imageData = imageData {
            let image = UIImage(data:imageData)
        }
    }
}

You can read more about storing and retrieving images stored as PFFile in Parse here:

https://parse.com/docs/ios/guide#files-images


Now, your next problem is that you have not known all of this while uploading the images in the first place. So there is a high risk that you've uploaded the images to Parse badly in the first place and that the images doesn't exist there at all.

Community
  • 1
  • 1
Kumuluzz
  • 2,012
  • 1
  • 13
  • 17
  • ok man. Sorry I was just digging myself a hole, lets try and do this right. editing with a screen. one question I am not 100% sure on: what exactly is "imgLink"? is it just a naming convention? I actually ended up deleting the class and remaking it just to make sure it was working or whatever. I don't need any form of adding images in app, just need to pull it. so I just clicked on add file from the website. – dcarney999 Jul 18 '15 at 11:39
  • `imgLink` is the name of the property. In your case, based on the print screen, it should be named `image` – Kumuluzz Jul 18 '15 at 11:51
  • http://stackoverflow.com/questions/25764618/swift-image-retrieving-from-parse-sdk-getting-crashed more or less helped me. let randmoNumber = Int(arc4random_uniform(UInt32(objects!.count))) that prints out a random number. so that is a start. all I want to do now is go like (following that example using thumbNail. I want like thumbNail[randomNumber]. says "PFFile does not have a member named 'subscript'" – dcarney999 Jul 18 '15 at 20:16
  • Okay I'm over the random part. just want to swipe through the array. – dcarney999 Jul 18 '15 at 21:24
  • Im going to try this UIScroll view and see if that works. – dcarney999 Jul 18 '15 at 22:08
  • You should just focus on the simplest scenario first before thinking about all that swiping stuff, which is to just download 1 image and display it in an imageview. Once you have this working, then you can start focusing on displaying a random image from the array – Kumuluzz Jul 18 '15 at 23:10
  • Ive got the single image, or rather its loading I have the whole array loaded and its just choosing an image to display – dcarney999 Jul 18 '15 at 23:43
  • cat photo uploaded every time I run the app it shows a random image, which i guess its based on the timing of when it initializes – dcarney999 Jul 19 '15 at 00:27
  • Since you have successfully managed to download and display a random image from your array, then it fulfils your original question: `How to convert PFObject array in image view?`. Instead you'll need to create a new question related to the swiping features. Feel free to post a link to that question here so I can follow up on it :) – Kumuluzz Jul 19 '15 at 08:45