diff --git a/Yep/ViewControllers/NewFeed/NewFeedViewController.swift b/Yep/ViewControllers/NewFeed/NewFeedViewController.swift index e4dbe591..a0fc8b41 100644 --- a/Yep/ViewControllers/NewFeed/NewFeedViewController.swift +++ b/Yep/ViewControllers/NewFeed/NewFeedViewController.swift @@ -10,6 +10,7 @@ import UIKit import Proposer import CoreLocation import MobileCoreServices +import Photos class NewFeedViewController: UIViewController { @@ -29,6 +30,8 @@ class NewFeedViewController: UIViewController { return imagePicker }() + var imageAssetSet: Set? + var mediaImages = [UIImage]() { didSet { dispatch_async(dispatch_get_main_queue()) { [weak self] in @@ -110,8 +113,13 @@ class NewFeedViewController: UIViewController { let vc = segue.destinationViewController as! PickPhotosViewController - vc.completion = { [weak self] images in - self?.mediaImages.appendContentsOf(images) + if let imageAssetSet = imageAssetSet { + vc.pickedImageSet = imageAssetSet + } + + vc.completion = { [weak self] images, imageAssetSet in + self?.mediaImages = images + self?.imageAssetSet = imageAssetSet } } } diff --git a/Yep/ViewControllers/PickPhotos/ImageCacheController.swift b/Yep/ViewControllers/PickPhotos/ImageCacheController.swift index ad8e0da8..26de1f0c 100644 --- a/Yep/ViewControllers/PickPhotos/ImageCacheController.swift +++ b/Yep/ViewControllers/PickPhotos/ImageCacheController.swift @@ -35,10 +35,8 @@ class ImageCacheController { updatedCache.addIndex(path.item) } - println("images: \(images), \(visibleCells.count), \(updatedCache.lastIndex), \(cachePreheatSize)") let minCache = max(0, updatedCache.firstIndex - cachePreheatSize) let maxCache = min(images.count - 1, updatedCache.lastIndex + cachePreheatSize) - println("cache: \(minCache), \(maxCache)") updatedCache.addIndexesInRange(NSMakeRange(minCache, maxCache - minCache + 1)) @@ -47,7 +45,7 @@ class ImageCacheController { if !updatedCache.containsIndex(index) { let asset: PHAsset! = self.images[index] as! PHAsset self.imageCache.stopCachingImagesForAssets([asset], targetSize: self.targetSize, contentMode: self.contentMode, options: nil) - print("Stopping caching image \(index)") + //println("Stopping caching image \(index)") } } @@ -56,7 +54,7 @@ class ImageCacheController { if !self.cachedIndices.containsIndex(index) { let asset: PHAsset! = self.images[index] as! PHAsset self.imageCache.startCachingImagesForAssets([asset], targetSize: self.targetSize, contentMode: self.contentMode, options: nil) - print("Starting caching image \(index)") + //println("Starting caching image \(index)") } } diff --git a/Yep/ViewControllers/PickPhotos/PickPhotosViewController.swift b/Yep/ViewControllers/PickPhotos/PickPhotosViewController.swift index 4af60dad..4b8ae0e6 100644 --- a/Yep/ViewControllers/PickPhotos/PickPhotosViewController.swift +++ b/Yep/ViewControllers/PickPhotos/PickPhotosViewController.swift @@ -17,7 +17,7 @@ class PickPhotosViewController: UICollectionViewController, PHPhotoLibraryChange var imageCacheController: ImageCacheController! var pickedImageSet = Set() - var completion: ((images: [UIImage]) -> Void)? + var completion: ((images: [UIImage], imageAssetSet: Set) -> Void)? let photoCellID = "PhotoCell" @@ -67,8 +67,8 @@ class PickPhotosViewController: UICollectionViewController, PHPhotoLibraryChange let pixelWidth = CGFloat(imageAsset.pixelWidth) let pixelHeight = CGFloat(imageAsset.pixelHeight) - println("pixelWidth: \(pixelWidth)") - println("pixelHeight: \(pixelHeight)") + //println("pixelWidth: \(pixelWidth)") + //println("pixelHeight: \(pixelHeight)") let targetSize: CGSize @@ -83,17 +83,17 @@ class PickPhotosViewController: UICollectionViewController, PHPhotoLibraryChange targetSize = CGSize(width: width, height: height) } - println("targetSize: \(targetSize)") + //println("targetSize: \(targetSize)") imageManager.requestImageForAsset(imageAsset, targetSize: targetSize, contentMode: .AspectFill, options: options) { image, info in if let image = image { - println("image.size: \(image.size)") + //println("image.size: \(image.size)") images.append(image) } } } - completion?(images: images) + completion?(images: images, imageAssetSet: pickedImageSet) navigationController?.popViewControllerAnimated(true) } @@ -108,18 +108,23 @@ class PickPhotosViewController: UICollectionViewController, PHPhotoLibraryChange } override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { + let cell = collectionView.dequeueReusableCellWithReuseIdentifier(photoCellID, forIndexPath: indexPath) as! PhotoCell - - cell.imageManager = imageManager - - if let imageAsset = images[indexPath.item] as? PHAsset { - cell.imageAsset = imageAsset - cell.photoPickedImageView.hidden = !pickedImageSet.contains(imageAsset) - } - return cell } + override func collectionView(collectionView: UICollectionView, willDisplayCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) { + + if let cell = cell as? PhotoCell { + cell.imageManager = imageManager + + if let imageAsset = images[indexPath.item] as? PHAsset { + cell.imageAsset = imageAsset + cell.photoPickedImageView.hidden = !pickedImageSet.contains(imageAsset) + } + } + } + override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { if let imageAsset = images[indexPath.item] as? PHAsset { diff --git a/Yep/Views/Cells/Feed/FeedCell.swift b/Yep/Views/Cells/Feed/FeedCell.swift index 850a5e36..7d9d3ea8 100644 --- a/Yep/Views/Cells/Feed/FeedCell.swift +++ b/Yep/Views/Cells/Feed/FeedCell.swift @@ -183,7 +183,7 @@ extension FeedCell: UICollectionViewDataSource, UICollectionViewDelegate { if let imageURL = attachmentURLs[safe: indexPath.item] { - println("attachment imageURL: \(imageURL)") + //println("attachment imageURL: \(imageURL)") cell.configureWithImageURL(imageURL) } diff --git a/Yep/Views/Cells/Photo/PhotoCell.swift b/Yep/Views/Cells/Photo/PhotoCell.swift index e2d9cb0f..e02f10de 100644 --- a/Yep/Views/Cells/Photo/PhotoCell.swift +++ b/Yep/Views/Cells/Photo/PhotoCell.swift @@ -16,15 +16,20 @@ class PhotoCell: UICollectionViewCell { var imageManager: PHImageManager? + static var imageRequestOptions: PHImageRequestOptions = { + let options = PHImageRequestOptions() + options.synchronous = true + return options + }() + var imageAsset: PHAsset? { willSet { guard let imageAsset = newValue else { return } - self.imageManager?.requestImageForAsset(imageAsset, targetSize: CGSize(width: 80, height: 80), contentMode: .AspectFill, options: nil) { [weak self] image, info in + self.imageManager?.requestImageForAsset(imageAsset, targetSize: CGSize(width: 120, height: 120), contentMode: .AspectFill, options: PhotoCell.imageRequestOptions) { [weak self] image, info in self?.photoImageView.image = image - self?.photoPickedImageView.hidden = !imageAsset.favorite } } } diff --git a/Yep/Views/Feed/FeedView.swift b/Yep/Views/Feed/FeedView.swift index e8713718..03856a85 100644 --- a/Yep/Views/Feed/FeedView.swift +++ b/Yep/Views/Feed/FeedView.swift @@ -229,7 +229,7 @@ extension FeedView: UICollectionViewDataSource, UICollectionViewDelegate { let imageURL = attachmentURLs[indexPath.item] - println("attachment imageURL: \(imageURL)") + //println("attachment imageURL: \(imageURL)") cell.configureWithImageURL(imageURL)