简体   繁体   中英

Appending image to array from DocumentDirectory

I am creating a UICollectionView. The user chooses an image from UIImagePicker, and then I save the image to the DocumentDirectory, but now I want to add the image to the UICollectionView. How can I access the document directory in the DetailViewController and add it to the array in the UICollectionView controller?

DetailViewController(Where I get and save the Image):

import UIKit

class DetailViewController: UIViewController, UITextFieldDelegate, UINavigationControllerDelegate, UIImagePickerControllerDelegate {

...

@IBAction func takePicture(sender: UIBarButtonItem) {

    let imagePicker = UIImagePickerController()

    if UIImagePickerController.isSourceTypeAvailable(.Camera) {
        imagePicker.sourceType = .Camera
    } else {
        imagePicker.sourceType = .PhotoLibrary
    }
    imagePicker.delegate = self

    presentViewController(imagePicker, animated: true, completion: nil)
}

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String: AnyObject]) {

    let img = info[UIImagePickerControllerOriginalImage] as! UIImage
    let data = UIImagePNGRepresentation(img)
    NSUserDefaults.standardUserDefaults().setObject(data, forKey: "myImageKey")
    NSUserDefaults.standardUserDefaults().synchronize()

    imageView.image = img

    dismissViewControllerAnimated(true, completion: nil)
}

...

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)

    let key = item.itemKey

    if let imgData = NSUserDefaults.standardUserDefaults().objectForKey("myImageKey") as? NSData {
        imageView.image = UIImage(data: imgData)
    }
    //imageView.image = UIImage(data: imgData)
}

...

UICollectionView:

import UIKit

class PhotosViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {

var collectionView: UICollectionView!
var imageStore: ImageStore!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
    layout.sectionInset = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
    layout.itemSize = CGSize(width: 300, height: 490)

    collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
    collectionView.dataSource = self
    collectionView.delegate = self
    collectionView!.registerClass(FoodCell.self, forCellWithReuseIdentifier: "Cell")
    collectionView.backgroundColor = UIColor.whiteColor()
    self.view.addSubview(collectionView)
}

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
}

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return images.count
}

var images: [UIImage] = [

]

images.append(UIImage(named: "" )!)

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! FoodCell
    cell.textLabel.text = ""
    cell.imageView.image = images[indexPath.row]
    return cell
}

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath)
{
    print("User tapped on item \(indexPath.row)")
}


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

You could use delegate mechanism to pass the picked image from DetailVC to the PhotosViewController . If you are new to delegate here is a rough implementation.

protocol DetailViewControllerDelegate{
    func didPickImage(image:UIImage)
}

class DetailViewController: UIViewController{
   var delegate: DetailViewControllerDelegate?
}

class PhotosViewController:DetailViewControllerDelegate{

    func didPickImage(image:UIImage){
        imagesArray.append(image)
        collectionView.reloadData()
    }
}


//not sure how you are navigating to DetailVC from PhotosVC
// before doing that set the delegate like

detailVC.delegate = self

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM