数据模型

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++

        })
    }
}

从该代码中,假设我有5个数据:
  • dataImage(userId:“img1”,值:1.0,照片:图片,裁剪照片:
    图片)
  • dataImage(userId:“img2”,值:1.0,照片:图片,裁剪照片:
    图片)
  • dataImage(userId:“img3”,值:1.0,照片:图片,裁剪照片:
    图片
    )
  • dataImage(userId:“img4”,值:1.0,照片:图片,裁剪照片:
    图片)
  • dataImage(userId:“img5”,值:1.0,照片:图片,裁剪照片:
    图片)

  • 我从视图控制器中选择“img3”,并将其成功移动到 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
        }
     }
    

    roppedViewController 中,我裁剪了“img3”图像。

    我的问题是,我希望裁剪后的图像在data [“img3”]中进行更新,然后将其移回 viewController 。这该怎么做?

    最佳答案

    看,您有数组datas。您可以执行以下操作:

    var datas = [DataImage]()
    class ViewController: UIViewController {
        override func viewDidLoad() {
            // fill your array with append or addObject
            print(datas) // without "self."
    }
    

    10-08 05:49