From 2cfa6d0c634ebe704ac8a80f2ef0986c08b62dbc Mon Sep 17 00:00:00 2001 From: nixzhu Date: Thu, 15 Oct 2015 20:11:15 +0800 Subject: [PATCH 1/4] fix logic of photoPickedImageView.hidden when scroll --- .../PickPhotos/PickPhotosViewController.swift | 21 ++++++++++++------- Yep/Views/Cells/Photo/PhotoCell.swift | 1 - 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/Yep/ViewControllers/PickPhotos/PickPhotosViewController.swift b/Yep/ViewControllers/PickPhotos/PickPhotosViewController.swift index 4af60dad..dcea1928 100644 --- a/Yep/ViewControllers/PickPhotos/PickPhotosViewController.swift +++ b/Yep/ViewControllers/PickPhotos/PickPhotosViewController.swift @@ -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/Photo/PhotoCell.swift b/Yep/Views/Cells/Photo/PhotoCell.swift index e2d9cb0f..27c5fc60 100644 --- a/Yep/Views/Cells/Photo/PhotoCell.swift +++ b/Yep/Views/Cells/Photo/PhotoCell.swift @@ -24,7 +24,6 @@ class PhotoCell: UICollectionViewCell { self.imageManager?.requestImageForAsset(imageAsset, targetSize: CGSize(width: 80, height: 80), contentMode: .AspectFill, options: nil) { [weak self] image, info in self?.photoImageView.image = image - self?.photoPickedImageView.hidden = !imageAsset.favorite } } } From c2be413001a75d2e8c2a5eb0672527b343362488 Mon Sep 17 00:00:00 2001 From: nixzhu Date: Thu, 15 Oct 2015 20:15:18 +0800 Subject: [PATCH 2/4] better logic for PhotoCell requestImage --- Yep/Views/Cells/Photo/PhotoCell.swift | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Yep/Views/Cells/Photo/PhotoCell.swift b/Yep/Views/Cells/Photo/PhotoCell.swift index 27c5fc60..e02f10de 100644 --- a/Yep/Views/Cells/Photo/PhotoCell.swift +++ b/Yep/Views/Cells/Photo/PhotoCell.swift @@ -16,13 +16,19 @@ 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 } } From cd105e9b3d1075867ad375dbd28ab545d9388ece Mon Sep 17 00:00:00 2001 From: nixzhu Date: Thu, 15 Oct 2015 20:18:54 +0800 Subject: [PATCH 3/4] clean some println --- Yep/ViewControllers/PickPhotos/ImageCacheController.swift | 6 ++---- Yep/Views/Cells/Feed/FeedCell.swift | 2 +- Yep/Views/Feed/FeedView.swift | 2 +- 3 files changed, 4 insertions(+), 6 deletions(-) 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/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/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) From 104720af927eec3fc8d4a9ffa9842831871c7762 Mon Sep 17 00:00:00 2001 From: nixzhu Date: Thu, 15 Oct 2015 20:32:54 +0800 Subject: [PATCH 4/4] better logic for pick photos, done, and pick photos again --- .../NewFeed/NewFeedViewController.swift | 12 ++++++++++-- .../PickPhotos/PickPhotosViewController.swift | 12 ++++++------ 2 files changed, 16 insertions(+), 8 deletions(-) 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/PickPhotosViewController.swift b/Yep/ViewControllers/PickPhotos/PickPhotosViewController.swift index dcea1928..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) }