2016-12-11 5 views
0

나는 UICollectionView를 만들고 있습니다. 사용자가 UIImagePicker에서 이미지를 선택한 다음 DocumentDirectory에 이미지를 저장하지만 이제 이미지를 UICollectionView에 추가하려고합니다. DetailViewController의 문서 디렉토리에 액세스하여 UICollectionView 컨트롤러의 배열에 추가하려면 어떻게해야합니까? (나는 얻을 이미지를 저장)DocumentDirectory에서 배열에 이미지 추가

DetailViewController :

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. 
    } 
} 

답변

0
당신은 PhotosViewController에 DetailVC에서 포착 된 이미지를 전달하는 delegate 메커니즘을 사용할 수

. 위임을 처음 사용하는 경우 대략적인 구현이 필요합니다.

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

이 시점에서 PhotosViewController는 ViewController에서 상속되고 DetailViewController에 대한 경로가 없으므로 작동하지 않았습니다. 이 방법을 사용하려면 어떻게해야합니까? – Allie