【问题标题】:Appending image to array from DocumentDirectory从 DocumentDirectory 将图像附加到数组
【发布时间】:2017-04-26 05:22:54
【问题描述】:

我正在创建一个 UICollectionView。用户从 UIImagePicker 中选择了一个图像,然后我将图像保存到 DocumentDirectory,但现在我想将图像添加到 UICollectionView。如何访问 DetailViewController 中的文档目录并将其添加到 UICollectionView 控制器中的数组中?

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

【问题讨论】:

    标签: ios arrays uiimage uicollectionview nsdocumentdirectory


    【解决方案1】:

    您可以使用delegate 机制将拾取的图像从DetailVC 传递到PhotosViewController。如果您是新手,在这里委托是一个粗略的实现。

    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
    

    【讨论】:

    • 这不起作用,因为此时 PhotosViewController 继承自 ViewController,并且没有到 DetailViewController 的路径。我应该如何建立这条路径才能让它发挥作用?
    猜你喜欢
    • 2017-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-05
    • 2020-12-31
    相关资源
    最近更新 更多