0

I use core Data to store my image as NSdata, so the image will be stored from another TableViewController

Now, I would try to put a image stored as a NSData into the pickerView

var restaurants:Restaurant!
var imageArray = [NSData]()

var dataArray1 = [Int]()

override func viewDidLoad() {
    super.viewDidLoad()

    imageArray = [restaurants.image!]

    for(var i = 0; i < 100; i++) {
        dataArray1.append((Int)(arc4random() % 10))
    }

    resultLabel.text = ""

    pickerView.delegate = self
    pickerView.dataSource = self
}

@IBAction func buttonClicked(sender: AnyObject) {
    pickerView.selectRow(Int(arc4random())%94 + 3, inComponent: 0, animated: true)

}

func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView?) -> UIView {

    let pickerImage = UIImageView()

    if component == 0 {
        _ = imageArray[(Int)(dataArray1[row])]
    }

    return pickerImage
}

it looks like the view below, but the image inside should be my NSData files http://www.appcoda.com.tw/wp-content/uploads/2015/08/emoji-slotmachine.gif

When I run it, it always shut down. I think the problem is that Array doesn't get the value from the Core Data? Really need help, please.

Jay Bhalani
  • 4,142
  • 8
  • 37
  • 50
HungCLo
  • 442
  • 2
  • 6
  • 21
  • What do you mean shutdown? Do you get an error message? Have you traced the failure to a specific piece of code; you need to slow things down and find the exact point it fails. – user3069232 Feb 27 '16 at 06:56
  • Thanks for your remind! I got a Thread 1: breakpoint 1.1 at line imageArray = [restaurant.image!] the fact is that I really don't know what it means! – HungCLo Feb 27 '16 at 08:35

1 Answers1

0

You are not properly converting your NSData representing a UIImage back to a UIImage. Because of this, the cast that you are trying to do fails every time.

Check out this question:

To summarize for you: To get a UIImage, you need this line somewhere:

image = [UIImage imageWithData:data];
Community
  • 1
  • 1
BHendricks
  • 4,423
  • 6
  • 32
  • 59