0

Trying to extract CGPoint values from my allCenters array, which contains NSValues. They were converted to NSValues from CGPoints initially. I know that this code is what I'm trying to do, but I guess it doesn't work in SWIFT 2: CGPoint randLoc = [[allCenters objectsAtIndex: randLocInt] CGPointValue]. I'm needing help on the last line of code, before the override funcs.

import UIKit
import Foundation
var tiledViewsStack = [AnyObject]()

class PhotoViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

    //Choose an image from Photo Library and display on screen in displayImageView

    @IBOutlet weak var displayImageView: UIImageView!

    @IBAction func choosePicFromLibrary(sender: AnyObject) {
        let imagePicker: UIImagePickerController = UIImagePickerController()

        imagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
        imagePicker.delegate = self
        imagePicker.modalPresentationStyle = UIModalPresentationStyle.Popover

        if (imagePicker.popoverPresentationController != nil) {
            imagePicker.popoverPresentationController!.sourceView = sender as! UIButton
            imagePicker.popoverPresentationController!.sourceRect = (sender as! UIButton).bounds
        }
        presentViewController(imagePicker, animated: true, completion: nil)
    }

    @IBAction func takePhoto(sender: AnyObject) {
        let imagePicker: UIImagePickerController = UIImagePickerController()

        imagePicker.sourceType = UIImagePickerControllerSourceType.Camera
        imagePicker.delegate = self
        imagePicker.modalPresentationStyle = UIModalPresentationStyle.Popover

        if (imagePicker.popoverPresentationController != nil) {
            imagePicker.popoverPresentationController!.sourceView = sender as! UIButton
            imagePicker.popoverPresentationController!.sourceRect = (sender as! UIButton).bounds
        }
        presentViewController(imagePicker, animated: true, completion: nil)
    }


    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
        dismissViewControllerAnimated(true, completion: nil)
        displayImageView.image = info[UIImagePickerControllerOriginalImage] as! UIImage!
    }

    func imagePickerControllerDidCancel(picker: UIImagePickerController) {
        dismissViewControllerAnimated(true, completion: nil)
    }



    //Cut photo into 9 tiles
    //Cut selected image into 9 pieces and add each cropped image to tileImageStack array

    var tileImageStack = [AnyObject]()

    @IBAction func randomize(sender: AnyObject) {

        let selectedImageWidth = displayImageView.image!.size.width
        let selectedImageHeight = displayImageView.image!.size.height

        let tileSize = CGSizeMake(selectedImageWidth/3, selectedImageHeight/3)

        for (var colI = 0; colI < 3; colI++)
        {
            for (var rowI = 0; rowI < 3; rowI += 1)
            {
                let tileRect = CGRectMake(CGFloat(rowI) * tileSize.width, tileSize.height * CGFloat(colI), tileSize.width, tileSize.height)

                if let selectedImage = displayImageView.image
                {
                    let tileImage = CGImageCreateWithImageInRect(selectedImage.CGImage, tileRect)
                    let aUItile = UIImage(CGImage: tileImage!)
                    tileImageStack.append(aUItile)
                }
            }
        }

        //Display tiles in order on screen, then mix them up randomly

        let frameWidth = self.view.frame.width
        let frameHeight = self.view.frame.height

        var xCen = (frameWidth/3)/2
        var yCen = (frameHeight/3)/2

        var pieceNumber = 0
        var allCenters = [NSValue]()

        for (var v = 0; v < 3; v += 1)
        {
            for (var h = 0; h < 3; h += 1)
            {
                let tiledView = UIImageView(frame:CGRectMake(0, 0, frameWidth/3, (frameHeight)/3))
                var curCenter = CGPointMake(xCen, yCen)
                allCenters.append(NSValue(CGPoint:curCenter))
                //tiledView.backgroundColor = UIColor.redColor()
                tiledView.center = curCenter
                tiledView.image = tileImageStack[pieceNumber] as? UIImage
                tiledView.userInteractionEnabled = true
                tiledViewsStack.append(tiledView)
                self.view.addSubview(tiledView)
                xCen += (frameWidth/3)
                pieceNumber += 1
            }
            xCen = (frameWidth/3)/2
            yCen += (frameHeight/3)
        }

        tiledViewsStack[0].removeFromSuperview()
        tiledViewsStack.removeAtIndex(0)
        //Now there are 8 imageViews in the tiledViewsStack array, and 9 centers stored in allCenters array.
    }

    var randLocInt = Int((arc4random()*10) % 9) // 0, --- 8

    //CGPoint randLoc = [[allCenters objectsAtIndex: randLocInt] CGPointValue]
    var randLoc = CGPointFromValue(allCenters[randLocInt])



    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }        
}

enter image description here

1 Answers1

1

If allCenters contains always NSValue objects why do you declare it as the quite unspecified [AnyObject]?

Help the compiler and the compiler will help you

var allCenters = [NSValue]()

...

let randLoc = allCenters[randLocInt].CGPointValue()

and declare allCenters inside the class

vadian
  • 274,689
  • 30
  • 353
  • 361
  • Good point, thanks. I still get the error: Use of unresolved identifier "CGPointFromValue". I also tried: `var randLoc = CGPoint(allCenters[randLocInt])`, but get this error: "Instance member 'randLocInt' cannot be used on type 'PhotoViewController'" – James D. Schwartz Apr 08 '16 at 20:40
  • There is no function `CGPointFromValue`. See my updated answer. – vadian Apr 08 '16 at 20:42
  • I still get "Instance member 'randLocInt' cannot be used on type 'PhotoViewController'" – James D. Schwartz Apr 08 '16 at 20:49
  • The line `let randLoc = allCenters[randLocInt].CGPointValue()` must be somewhere in a method. You can't use it on the top level of the class. Probably the closing brace of the `randomize` method is supposed to be after that line. – vadian Apr 08 '16 at 20:53
  • Hmm. Okay. When I put the CGPoint values INTO the array as NSValues with `allCenters.append(NSValue(CGPoint:curCenter))`, isn't there a similar way to get them back out? – James D. Schwartz Apr 08 '16 at 20:55
  • What do you mean with *similar way to get them back out*? You can access the items in the array by index. – vadian Apr 08 '16 at 20:59
  • See added screenshot. I declared `allCenters` at the top, error at the bottom. – James D. Schwartz Apr 08 '16 at 21:04
  • 1
    Once again: **The line `let randLoc = allCenters[randLocInt].CGPointValue()` must be somewhere inside a method.** – vadian Apr 08 '16 at 21:06