使用照片构buildSQLite数据库

我打算在UICollectionView控制器中显示用户从DetailViewController收集的图像。 我想使用一个SQLite数据库,但我不确定如何启动它,看到我已经有大部分的应用程序build立和build立。 下面看到我的DetailViewController(图像收集和显示),ImageStore.swift(图像当前正在存储)和UICollectionView控制器。

ImageStore.swift:

class ImageStore: NSObject { let cache = NSCache() func setImage(image: UIImage, forKey key: String) { cache.setObject(image, forKey: key) let imageURL = imageURLForKey(key) if let data = UIImageJPEGRepresentation(image, 0.5) { data.writeToURL(imageURL, atomically: true) } } func imageForKey(key: String) -> UIImage? { if let existingImage = cache.objectForKey(key) as? UIImage { return existingImage } let imageURL = imageURLForKey(key) guard let imageFromDisk = UIImage(contentsOfFile: imageURL.path!) else { return nil } cache.setObject(imageFromDisk, forKey: key) return imageFromDisk } func deleteImageForKey(key: String) { cache.removeObjectForKey(key) let imageURL = imageURLForKey(key) do { try NSFileManager.defaultManager().removeItemAtURL(imageURL) } catch let deleteError { print("Error removing the image from disk: \(deleteError)") } } func imageURLForKey(key: String) -> NSURL { let documentsDirectories = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask) let documentDirectory = documentsDirectories.first! return documentDirectory.URLByAppendingPathComponent(key) } 

DetailViewController:

 var imageStore: ImageStore! @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 image = info[UIImagePickerControllerOriginalImage] as! UIImage imageStore.setImage(image, forKey: item.itemKey) imageView.image = image dismissViewControllerAnimated(true, completion: nil) } 

UICollectionView:

 class PhotosViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource { override func viewDidLoad() { super.viewDidLoad() } override func viewWillAppear(animated: Bool) { super.viewWillAppear(animated) let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout() layout.sectionInset = UIEdgeInsets(top: 20, left: 10, bottom: 10, right: 10) layout.itemSize = CGSize(width: 100, height: 100) let myCollectionView:UICollectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout) myCollectionView.dataSource = self myCollectionView.delegate = self myCollectionView.registerClass(RDCellCollectionViewCell.self, forCellWithReuseIdentifier: "MyCell") myCollectionView.backgroundColor = UIColor.whiteColor() self.view.addSubview(myCollectionView) } func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return images.count } var images: [UIImage] = [ ] func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { let myCell = collectionView.dequeueReusableCellWithReuseIdentifier("MyCell", forIndexPath: indexPath) as! RDCellCollectionViewCell myCell.imageView.image = images[indexPath.item] return myCell } 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. }