3

Data Model

class DataImage {
    var userId: String
    var value: Double
    var photo: UIImage?
    var croppedPhoto: UIImage?

init(userId:String, value: Double, photo: UIImage?, croppedPhoto: UIImage?){
    self.userId = userId
    self.value = value
    self.photo = photo
    self.photo = croppedPhoto
   }

}

ViewController(tableView)

var photos = [DKAsset]() //image source
var datas = [DataImage]()
override func viewDidLoad() {
    super.viewDidLoad()
        self.loadDataPhoto()
}

func loadDataPhoto(){
  var counter = 0
    for asset in photos{
        asset.fetchOriginalImageWithCompleteBlock({ image, info in // move image from photos to datas

            let images = image
            let data1 = dataImage(userId: "img\(counter+1)", value: 1.0, photo: images, croppedPhoto: images)
            self.datas += [data1]
            counter++

        })
    }
}

from that code, let's say i have 5 datas:

  • dataImage(userId: "img1", value: 1.0, photo: images, croppedPhoto: images)
  • dataImage(userId: "img2", value: 1.0, photo: images, croppedPhoto: images)
  • dataImage(userId: "img3", value: 1.0, photo: images, croppedPhoto: images)
  • dataImage(userId: "img4", value: 1.0, photo: images, croppedPhoto: images)
  • dataImage(userId: "img5", value: 1.0, photo: images, croppedPhoto: images)

and i choose "img3" from view controller and successfully move it to cropviewController

CropViewController

var datasCrop: dataImage?
override func viewDidLoad() {
    super.viewDidLoad()
    if let datasCrop = datasCrop{
        // beforeImage.image = datasCrop.photo
        photoId.text = datasCrop.userId
        self.cropView.imageToCrop = datasCrop.photo

    }
}

@IBAction func handleCropButton(sender: UIButton)
{
    if let croppedImage = cropView.croppedImage()
    {
      croppedImage //theCropped img3's cropped image
    }
 }

in croppedViewController, i crop the "img3" image.

My question is, I want that cropped image to be update in datas["img3"] and move it back to viewController. how to do this?

9
  • I am lazy to type the answer :D. But you can review this link stackoverflow.com/questions/5210535/…. It tells you how to pass object between view controller Commented Jan 21, 2016 at 4:58
  • do you have any swift source? @t4nhpt Commented Jan 21, 2016 at 5:08
  • Please see this link for Swift: stackoverflow.com/questions/25589240/… Commented Jan 21, 2016 at 5:32
  • You can try to make your array global and access to it from everywhere or you can access to it with ViewController().yourArray Commented Jan 21, 2016 at 6:28
  • @iamalizade oh so i could use ViewController().datas in 'handleCropButton'? im going to try it now Commented Jan 21, 2016 at 6:32

2 Answers 2

1

Look, you have the array datas. You can do the next:

var datas = [DataImage]()
class ViewController: UIViewController {
    override func viewDidLoad() {
        // fill your array with append or addObject
        print(datas) // without "self."
}
Sign up to request clarification or add additional context in comments.

Comments

0

You can use NSNotification or delegate methods to pass information between those two viewcontrollers Try this answer

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.