From d96fe546519b7274cdbf573a7423500be29211ea Mon Sep 17 00:00:00 2001 From: nixzhu Date: Mon, 2 Nov 2015 16:43:28 +0800 Subject: [PATCH 1/4] better tryShowMessageMediaFromMessage --- .../Conversation/ConversationViewController.swift | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/Yep/ViewControllers/Conversation/ConversationViewController.swift b/Yep/ViewControllers/Conversation/ConversationViewController.swift index 0af952b7..8f31e178 100644 --- a/Yep/ViewControllers/Conversation/ConversationViewController.swift +++ b/Yep/ViewControllers/Conversation/ConversationViewController.swift @@ -2765,15 +2765,20 @@ extension ConversationViewController: UICollectionViewDataSource, UICollectionVi private func tryShowMessageMediaFromMessage(message: Message) { - let predicate = NSPredicate(format: "mediaType = %d OR mediaType = %d", MessageMediaType.Image.rawValue, MessageMediaType.Video.rawValue) + if message.mediaType == MessageMediaType.Video.rawValue { + performSegueWithIdentifier("showMessageMedia", sender: ["mediaMessages": [message], "index": 0]) - let mediaMessagesResult = messages.filter(predicate) + } else { + let predicate = NSPredicate(format: "mediaType = %d", MessageMediaType.Image.rawValue) - let mediaMessages = mediaMessagesResult.map({ $0 }) + let mediaMessagesResult = messages.filter(predicate) - if let index = mediaMessagesResult.indexOf(message) { + let mediaMessages = mediaMessagesResult.map({ $0 }) - performSegueWithIdentifier("showMessageMedia", sender: ["mediaMessages": mediaMessages, "index": index]) + if let index = mediaMessagesResult.indexOf(message) { + + performSegueWithIdentifier("showMessageMedia", sender: ["mediaMessages": mediaMessages, "index": index]) + } } } From 9bc60955a9923fa4b1a3f6f7ef20a30b47fc9ffa Mon Sep 17 00:00:00 2001 From: nixzhu Date: Mon, 2 Nov 2015 17:29:21 +0800 Subject: [PATCH 2/4] refactor of use DiscoveredAttachment instead of NSURL for feed attachments --- Yep.xcodeproj/project.pbxproj | 4 ++ Yep/Caches/ImageCache.swift | 8 ++- Yep/Helpers/YepHelpers.swift | 17 +++++++ .../ConversationViewController.swift | 9 ++-- .../Feeds/FeedsViewController.swift | 11 ++-- .../MessageMediaViewController.swift | 8 +-- Yep/Views/Cells/Feed/FeedCell.swift | 22 ++++---- .../FeedConversationCell.swift | 7 +-- Yep/Views/Cells/FeedMedia/FeedMediaCell.swift | 16 +++--- Yep/Views/Feed/FeedMediaView.swift | 50 ++++++++++--------- Yep/Views/Feed/FeedView.swift | 23 ++++----- 11 files changed, 102 insertions(+), 73 deletions(-) create mode 100644 Yep/Helpers/YepHelpers.swift diff --git a/Yep.xcodeproj/project.pbxproj b/Yep.xcodeproj/project.pbxproj index 277bc3cf..af3e9a68 100644 --- a/Yep.xcodeproj/project.pbxproj +++ b/Yep.xcodeproj/project.pbxproj @@ -248,6 +248,7 @@ 50DEE0371BE3720F00015741 /* LoadMoreCollectionViewCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = 50DEE0351BE3720F00015741 /* LoadMoreCollectionViewCell.xib */; }; 50DEE03D1BE70E1500015741 /* UIScrollView+Yep.swift in Sources */ = {isa = PBXBuildFile; fileRef = 50DEE03C1BE70E1500015741 /* UIScrollView+Yep.swift */; }; 50DEE03F1BE729FD00015741 /* PickLocation.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 50DEE03E1BE729FD00015741 /* PickLocation.storyboard */; }; + 50DEE0411BE7626100015741 /* YepHelpers.swift in Sources */ = {isa = PBXBuildFile; fileRef = 50DEE0401BE7626100015741 /* YepHelpers.swift */; }; 50E114F41ABBF9CC00F13000 /* Models.swift in Sources */ = {isa = PBXBuildFile; fileRef = 50E114F31ABBF9CC00F13000 /* Models.swift */; }; 50E114F91ABC137A00F13000 /* YepServiceSync.swift in Sources */ = {isa = PBXBuildFile; fileRef = 50E114F81ABC137A00F13000 /* YepServiceSync.swift */; }; 50E114FC1ABC549300F13000 /* ContactsCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 50E114FA1ABC549300F13000 /* ContactsCell.swift */; }; @@ -567,6 +568,7 @@ 50DEE0351BE3720F00015741 /* LoadMoreCollectionViewCell.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; name = LoadMoreCollectionViewCell.xib; path = Views/Cells/LoadMore/LoadMoreCollectionViewCell.xib; sourceTree = ""; }; 50DEE03C1BE70E1500015741 /* UIScrollView+Yep.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = "UIScrollView+Yep.swift"; path = "Extensions/UIScrollView+Yep.swift"; sourceTree = ""; }; 50DEE03E1BE729FD00015741 /* PickLocation.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; name = PickLocation.storyboard; path = ViewControllers/PickLocation/PickLocation.storyboard; sourceTree = ""; }; + 50DEE0401BE7626100015741 /* YepHelpers.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = YepHelpers.swift; path = Helpers/YepHelpers.swift; sourceTree = ""; }; 50E114F31ABBF9CC00F13000 /* Models.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Models.swift; path = Realm/Models.swift; sourceTree = ""; }; 50E114F81ABC137A00F13000 /* YepServiceSync.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = YepServiceSync.swift; path = Services/YepServiceSync.swift; sourceTree = ""; }; 50E114FA1ABC549300F13000 /* ContactsCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = ContactsCell.swift; path = Views/Cells/Contacts/ContactsCell.swift; sourceTree = ""; }; @@ -1199,6 +1201,7 @@ 502AE53B1AB871C5005BD199 /* Helpers */ = { isa = PBXGroup; children = ( + 50DEE0401BE7626100015741 /* YepHelpers.swift */, 502AE5391AB871C1005BD199 /* YepAlert.swift */, 502AE53C1AB87C0D005BD199 /* YepUserDefaults.swift */, 50EB8C5D1AE78AB9001AC1EE /* YepAsset.swift */, @@ -2468,6 +2471,7 @@ 5064F6541B0B0E160089FAD4 /* AddFriendSearchCell.swift in Sources */, 504CBA3D1B3D04B800663A90 /* ChatTextView.swift in Sources */, 0A1CAC6E1AFA42D400826B45 /* SkillHomeViewController.swift in Sources */, + 50DEE0411BE7626100015741 /* YepHelpers.swift in Sources */, 501138311BCB62A20055A014 /* FeedConversationCell.swift in Sources */, 502AE5401AB88862005BD199 /* SkillRankCell.swift in Sources */, 50C090A01ADE391600CC6389 /* AddSkillsReusableView.swift in Sources */, diff --git a/Yep/Caches/ImageCache.swift b/Yep/Caches/ImageCache.swift index c8cef249..17415213 100644 --- a/Yep/Caches/ImageCache.swift +++ b/Yep/Caches/ImageCache.swift @@ -20,8 +20,12 @@ class ImageCache { let cacheAttachmentQueue = dispatch_queue_create("ImageCacheAttachmentQueue", DISPATCH_QUEUE_SERIAL) // let cacheQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0) - func imageOfAttachment(attachmentURL: NSURL, withSize: CGSize?, completion: (url: NSURL, image: UIImage?) -> Void) { - + func imageOfAttachment(attachment: DiscoveredAttachment, withSize: CGSize?, completion: (url: NSURL, image: UIImage?) -> Void) { + + guard let attachmentURL = NSURL(string: attachment.URLString) else { + return + } + var cacheSize = CGSizeZero if let withSize = withSize { diff --git a/Yep/Helpers/YepHelpers.swift b/Yep/Helpers/YepHelpers.swift new file mode 100644 index 00000000..b4221d49 --- /dev/null +++ b/Yep/Helpers/YepHelpers.swift @@ -0,0 +1,17 @@ +// +// YepHelpers.swift +// Yep +// +// Created by nixzhu on 15/11/2. +// Copyright © 2015年 Catch Inc. All rights reserved. +// + +import Foundation + +final class Box { + let value: T + + init(value: T) { + self.value = value + } +} \ No newline at end of file diff --git a/Yep/ViewControllers/Conversation/ConversationViewController.swift b/Yep/ViewControllers/Conversation/ConversationViewController.swift index 8f31e178..84637973 100644 --- a/Yep/ViewControllers/Conversation/ConversationViewController.swift +++ b/Yep/ViewControllers/Conversation/ConversationViewController.swift @@ -1235,10 +1235,10 @@ class ConversationViewController: BaseViewController { } } - feedView.tapMediaAction = { [weak self] transitionView, imageURLs, index in + feedView.tapMediaAction = { [weak self] transitionView, attachments, index in let info = [ "transitionView": transitionView, - "imageURLs": imageURLs, + "attachments": Box(value: attachments), "index": index, ] self?.performSegueWithIdentifier("showFeedMedia", sender: info) @@ -2304,8 +2304,9 @@ class ConversationViewController: BaseViewController { let vc = segue.destinationViewController as! MessageMediaViewController - if let imageURLs = info["imageURLs"] as? [NSURL] { - vc.previewMedias = imageURLs.map({ PreviewMedia.AttachmentType(imageURL: $0) }) + if let box = info["attachments"] as? Box<[DiscoveredAttachment]> { + let attachments = box.value + vc.previewMedias = attachments.map({ PreviewMedia.AttachmentType(attachment: $0) }) } if let index = info["index"] as? Int { diff --git a/Yep/ViewControllers/Feeds/FeedsViewController.swift b/Yep/ViewControllers/Feeds/FeedsViewController.swift index 74f0c377..db9ca38c 100644 --- a/Yep/ViewControllers/Feeds/FeedsViewController.swift +++ b/Yep/ViewControllers/Feeds/FeedsViewController.swift @@ -436,8 +436,9 @@ class FeedsViewController: UIViewController { let vc = segue.destinationViewController as! MessageMediaViewController - if let imageURLs = info["imageURLs"] as? [NSURL] { - vc.previewMedias = imageURLs.map({ PreviewMedia.AttachmentType(imageURL: $0) }) + if let box = info["attachments"] as? Box<[DiscoveredAttachment]> { + let attachments = box.value + vc.previewMedias = attachments.map({ PreviewMedia.AttachmentType(attachment: $0) }) } if let index = info["index"] as? Int { @@ -546,10 +547,10 @@ extension FeedsViewController: UITableViewDataSource, UITableViewDelegate { } } - cell.tapMediaAction = { [weak self] transitionView, imageURLs, index in - let info = [ + cell.tapMediaAction = { [weak self] transitionView, attachments, index in + let info: [String: AnyObject] = [ "transitionView": transitionView, - "imageURLs": imageURLs, + "attachments": Box(value: attachments), "index": index, ] self?.performSegueWithIdentifier("showFeedMedia", sender: info) diff --git a/Yep/ViewControllers/MessageMedia/MessageMediaViewController.swift b/Yep/ViewControllers/MessageMedia/MessageMediaViewController.swift index 669986db..82b29360 100644 --- a/Yep/ViewControllers/MessageMedia/MessageMediaViewController.swift +++ b/Yep/ViewControllers/MessageMedia/MessageMediaViewController.swift @@ -13,7 +13,7 @@ import MonkeyKing enum PreviewMedia { case MessageType(message: Message) - case AttachmentType(imageURL: NSURL) + case AttachmentType(attachment: DiscoveredAttachment) } class MessageMediaViewController: UIViewController { @@ -242,12 +242,12 @@ extension MessageMediaViewController: UICollectionViewDataSource, UICollectionVi break } - case .AttachmentType(let imageURL): + case .AttachmentType(let attachment): mediaControlView.type = .Image - ImageCache.sharedInstance.imageOfAttachment(imageURL, withSize: nil, completion: { (url, image) in - guard url == imageURL else { + ImageCache.sharedInstance.imageOfAttachment(attachment, withSize: nil, completion: { (url, image) in + guard url.absoluteString == attachment.URLString else { return } cell.mediaView.image = image diff --git a/Yep/Views/Cells/Feed/FeedCell.swift b/Yep/Views/Cells/Feed/FeedCell.swift index f8cdda73..7a24ec7d 100644 --- a/Yep/Views/Cells/Feed/FeedCell.swift +++ b/Yep/Views/Cells/Feed/FeedCell.swift @@ -30,15 +30,15 @@ class FeedCell: UITableViewCell { var tapAvatarAction: (UITableViewCell -> Void)? var tapSkillAction: (UITableViewCell -> Void)? - var tapMediaAction: ((transitionView: UIView, imageURLs: [NSURL], index: Int) -> Void)? + var tapMediaAction: ((transitionView: UIView, attachments: [DiscoveredAttachment], index: Int) -> Void)? var touchesBeganAction: (UITableViewCell -> Void)? var touchesEndedAction: (UITableViewCell -> Void)? var touchesCancelledAction: (UITableViewCell -> Void)? - var attachmentURLs = [NSURL]() { + var attachments = [DiscoveredAttachment]() { didSet { - if attachmentURLs.count == 1 { + if attachments.count == 1 { collectionViewHeight.constant = 160 } else { collectionViewHeight.constant = 80 @@ -142,7 +142,7 @@ class FeedCell: UITableViewCell { override func prepareForReuse() { super.prepareForReuse() - attachmentURLs = [] + attachments = [] messageTextView.text = nil messageTextView.attributedText = nil @@ -191,7 +191,7 @@ class FeedCell: UITableViewCell { mediaCollectionView.hidden = hasMedia ? false : true - attachmentURLs = feed.attachments.map({ NSURL(string: $0.URLString) }).flatMap({ $0 }) + attachments = feed.attachments let plainAvatar = PlainAvatar(avatarURLString: feed.creator.avatarURLString, avatarStyle: nanoAvatarStyle) avatarImageView.navi_setAvatar(plainAvatar) @@ -214,20 +214,18 @@ extension FeedCell: UICollectionViewDataSource, UICollectionViewDelegate { } func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { - return attachmentURLs.count + return attachments.count } func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCellWithReuseIdentifier(feedMediaCellID, forIndexPath: indexPath) as! FeedMediaCell - if let imageURL = attachmentURLs[safe: indexPath.item] { + if let attachment = attachments[safe: indexPath.item] { //println("attachment imageURL: \(imageURL)") - cell.attachmentURL = imageURL - - cell.configureWithImageURL(imageURL, bigger: (attachmentURLs.count == 1)) + cell.configureWithAttachment(attachment, bigger: (attachments.count == 1)) } return cell @@ -235,7 +233,7 @@ extension FeedCell: UICollectionViewDataSource, UICollectionViewDelegate { func collectionView(collectionView: UICollectionView!, layout collectionViewLayout: UICollectionViewLayout!, sizeForItemAtIndexPath indexPath: NSIndexPath!) -> CGSize { - if attachmentURLs.count > 1 { + if attachments.count > 1 { return CGSize(width: 80, height: 80) } else { @@ -252,7 +250,7 @@ extension FeedCell: UICollectionViewDataSource, UICollectionViewDelegate { let cell = collectionView.cellForItemAtIndexPath(indexPath) as! FeedMediaCell let transitionView = cell.imageView - tapMediaAction?(transitionView: transitionView, imageURLs: attachmentURLs, index: indexPath.item) + tapMediaAction?(transitionView: transitionView, attachments: attachments, index: indexPath.item) } } diff --git a/Yep/Views/Cells/FeedConversation/FeedConversationCell.swift b/Yep/Views/Cells/FeedConversation/FeedConversationCell.swift index 227af6e2..4a284b48 100644 --- a/Yep/Views/Cells/FeedConversation/FeedConversationCell.swift +++ b/Yep/Views/Cells/FeedConversation/FeedConversationCell.swift @@ -73,9 +73,10 @@ class FeedConversationCell: UITableViewCell { // timeLabel.text = "" // } - let attachmentURLs = feed.attachments.map({ NSURL(string: $0.URLString) }).flatMap({ $0 }) - mediaView.FeedURLs = attachmentURLs - mediaView.setImagesWithURLs(attachmentURLs) + let attachments = feed.attachments.map({ + DiscoveredAttachment(kind: AttachmentKind(rawValue: $0.kind)!, metadata: $0.metadata, URLString: $0.URLString) + })//.map({ NSURL(string: $0.URLString) }).flatMap({ $0 }) + mediaView.setImagesWithAttachments(attachments) if let latestMessage = messagesInConversation(conversation).last { diff --git a/Yep/Views/Cells/FeedMedia/FeedMediaCell.swift b/Yep/Views/Cells/FeedMedia/FeedMediaCell.swift index 0c253119..e830b162 100644 --- a/Yep/Views/Cells/FeedMedia/FeedMediaCell.swift +++ b/Yep/Views/Cells/FeedMedia/FeedMediaCell.swift @@ -27,15 +27,17 @@ class FeedMediaCell: UICollectionViewCell { deleteImageView.hidden = false } - func configureWithImageURL(imageURL: NSURL, bigger: Bool) { + func configureWithAttachment(attachment: DiscoveredAttachment, bigger: Bool) { + + attachmentURL = NSURL(string: attachment.URLString)! if bigger { // imageView.kf_setImageWithURL(imageURL, placeholderImage: YepConfig.FeedMedia.biggerPlaceholderImage) imageView.image = YepConfig.FeedMedia.biggerPlaceholderImage - ImageCache.sharedInstance.imageOfAttachment(imageURL, withSize: imageView.frame.size, completion: { [weak self] (url, image) in + ImageCache.sharedInstance.imageOfAttachment(attachment, withSize: imageView.frame.size, completion: { [weak self] (url, image) in - if let StrongSelf = self { - if StrongSelf.attachmentURL != url { + if let strongSelf = self { + if strongSelf.attachmentURL != url { return } } @@ -47,9 +49,9 @@ class FeedMediaCell: UICollectionViewCell { imageView.image = YepConfig.FeedMedia.placeholderImage // imageView.kf_setImageWithURL(imageURL, placeholderImage: YepConfig.FeedMedia.placeholderImage) - ImageCache.sharedInstance.imageOfAttachment(imageURL, withSize: imageView.frame.size, completion: { [weak self] (url, image) in - if let StrongSelf = self { - if StrongSelf.attachmentURL != url { + ImageCache.sharedInstance.imageOfAttachment(attachment, withSize: imageView.frame.size, completion: { [weak self] (url, image) in + if let strongSelf = self { + if strongSelf.attachmentURL != url { return } } diff --git a/Yep/Views/Feed/FeedMediaView.swift b/Yep/Views/Feed/FeedMediaView.swift index 995d37df..267a79c7 100644 --- a/Yep/Views/Feed/FeedMediaView.swift +++ b/Yep/Views/Feed/FeedMediaView.swift @@ -10,7 +10,7 @@ import UIKit class FeedMediaView: UIView { - var FeedURLs = [NSURL]() + var attachmentURLs = [NSURL]() lazy var imageView1: UIImageView = { let view = UIImageView() @@ -44,24 +44,26 @@ class FeedMediaView: UIView { return view }() - func setImagesWithURLs(URLs: [NSURL]) { + func setImagesWithAttachments(attachments: [DiscoveredAttachment]) { let fullRect = bounds let halfRect = CGRect(x: 0, y: 0, width: fullRect.width * 0.5, height: fullRect.height) let quarterRect = CGRect(x: 0, y: 0, width: fullRect.width * 0.5, height: fullRect.height * 0.5) - hidden = (URLs.count == 0) + hidden = (attachments.count == 0) // println(URLs) - switch URLs.count { + attachmentURLs = attachments.map({ NSURL(string: $0.URLString) }).flatMap({ $0 }) + + switch attachments.count { case 1: imageView1.frame = fullRect // imageView1.kf_setImageWithURL(URLs[0]) - ImageCache.sharedInstance.imageOfAttachment(URLs[0], withSize: fullRect.size, completion: { [weak self] (url, image) in - guard let StrongSelf = self where StrongSelf.FeedURLs.contains(url) else { + ImageCache.sharedInstance.imageOfAttachment(attachments[0], withSize: fullRect.size, completion: { [weak self] (url, image) in + guard let strongSelf = self where strongSelf.attachmentURLs.contains(url) else { return } self?.imageView1.image = image @@ -73,8 +75,8 @@ class FeedMediaView: UIView { imageView1.frame = halfRect imageView1.center = CGPoint(x: halfRect.width * 0.5, y: imageView1.center.y) // imageView1.kf_setImageWithURL(URLs[0]) - ImageCache.sharedInstance.imageOfAttachment(URLs[0], withSize: halfRect.size, completion: { [weak self] (url, image) in - guard let StrongSelf = self where StrongSelf.FeedURLs.contains(url) else { + ImageCache.sharedInstance.imageOfAttachment(attachments[0], withSize: halfRect.size, completion: { [weak self] (url, image) in + guard let strongSelf = self where strongSelf.attachmentURLs.contains(url) else { return } self?.imageView1.image = image @@ -83,8 +85,8 @@ class FeedMediaView: UIView { imageView2.frame = halfRect imageView2.center = CGPoint(x: halfRect.width * 1.5, y: imageView2.center.y) // imageView2.kf_setImageWithURL(URLs[1]) - ImageCache.sharedInstance.imageOfAttachment(URLs[1], withSize: halfRect.size, completion: { [weak self] (url, image) in - guard let StrongSelf = self where StrongSelf.FeedURLs.contains(url) else { + ImageCache.sharedInstance.imageOfAttachment(attachments[1], withSize: halfRect.size, completion: { [weak self] (url, image) in + guard let strongSelf = self where strongSelf.attachmentURLs.contains(url) else { return } self?.imageView2.image = image @@ -96,8 +98,8 @@ class FeedMediaView: UIView { case 3: imageView1.frame = quarterRect // imageView1.kf_setImageWithURL(URLs[0]) - ImageCache.sharedInstance.imageOfAttachment(URLs[0], withSize: quarterRect.size, completion: { [weak self] (url, image) in - guard let StrongSelf = self where StrongSelf.FeedURLs.contains(url) else { + ImageCache.sharedInstance.imageOfAttachment(attachments[0], withSize: quarterRect.size, completion: { [weak self] (url, image) in + guard let strongSelf = self where strongSelf.attachmentURLs.contains(url) else { return } self?.imageView1.image = image @@ -107,8 +109,8 @@ class FeedMediaView: UIView { imageView2.center = CGPoint(x: imageView2.center.x, y: quarterRect.height * 1.5) // imageView2.kf_setImageWithURL(URLs[1]) - ImageCache.sharedInstance.imageOfAttachment(URLs[1], withSize: quarterRect.size, completion: { [weak self] (url, image) in - guard let StrongSelf = self where StrongSelf.FeedURLs.contains(url) else { + ImageCache.sharedInstance.imageOfAttachment(attachments[1], withSize: quarterRect.size, completion: { [weak self] (url, image) in + guard let strongSelf = self where strongSelf.attachmentURLs.contains(url) else { return } self?.imageView2.image = image @@ -118,8 +120,8 @@ class FeedMediaView: UIView { imageView3.center = CGPoint(x: halfRect.width * 1.5, y: imageView3.center.y) // imageView3.kf_setImageWithURL(URLs[2]) - ImageCache.sharedInstance.imageOfAttachment(URLs[2], withSize: quarterRect.size, completion: { [weak self] (url, image) in - guard let StrongSelf = self where StrongSelf.FeedURLs.contains(url) else { + ImageCache.sharedInstance.imageOfAttachment(attachments[2], withSize: quarterRect.size, completion: { [weak self] (url, image) in + guard let strongSelf = self where strongSelf.attachmentURLs.contains(url) else { return } self?.imageView3.image = image @@ -134,8 +136,8 @@ class FeedMediaView: UIView { imageView1.frame = quarterRect // imageView1.kf_setImageWithURL(URLs[0]) - ImageCache.sharedInstance.imageOfAttachment(URLs[0], withSize: quarterRect.size, completion: { [weak self] (url, image) in - guard let StrongSelf = self where StrongSelf.FeedURLs.contains(url) else { + ImageCache.sharedInstance.imageOfAttachment(attachments[0], withSize: quarterRect.size, completion: { [weak self] (url, image) in + guard let strongSelf = self where strongSelf.attachmentURLs.contains(url) else { return } self?.imageView1.image = image @@ -145,8 +147,8 @@ class FeedMediaView: UIView { imageView2.center = CGPoint(x: imageView2.center.x, y: quarterRect.height * 1.5) // imageView2.kf_setImageWithURL(URLs[1]) - ImageCache.sharedInstance.imageOfAttachment(URLs[1], withSize: quarterRect.size, completion: { [weak self] (url, image) in - guard let StrongSelf = self where StrongSelf.FeedURLs.contains(url) else { + ImageCache.sharedInstance.imageOfAttachment(attachments[1], withSize: quarterRect.size, completion: { [weak self] (url, image) in + guard let strongSelf = self where strongSelf.attachmentURLs.contains(url) else { return } self?.imageView2.image = image @@ -155,8 +157,8 @@ class FeedMediaView: UIView { imageView3.frame = quarterRect imageView3.center = CGPoint(x: quarterRect.width * 1.5, y: imageView3.center.y) // imageView3.kf_setImageWithURL(URLs[2]) - ImageCache.sharedInstance.imageOfAttachment(URLs[2], withSize: quarterRect.size, completion: { [weak self] (url, image) in - guard let StrongSelf = self where StrongSelf.FeedURLs.contains(url) else { + ImageCache.sharedInstance.imageOfAttachment(attachments[2], withSize: quarterRect.size, completion: { [weak self] (url, image) in + guard let strongSelf = self where strongSelf.attachmentURLs.contains(url) else { return } self?.imageView3.image = image @@ -166,8 +168,8 @@ class FeedMediaView: UIView { imageView4.center = CGPoint(x: quarterRect.width * 1.5, y: quarterRect.height * 1.5) // imageView4.kf_setImageWithURL(URLs[3]) - ImageCache.sharedInstance.imageOfAttachment(URLs[3], withSize: quarterRect.size, completion: { [weak self] (url, image) in - guard let StrongSelf = self where StrongSelf.FeedURLs.contains(url) else { + ImageCache.sharedInstance.imageOfAttachment(attachments[3], withSize: quarterRect.size, completion: { [weak self] (url, image) in + guard let strongSelf = self where strongSelf.attachmentURLs.contains(url) else { return } self?.imageView4.image = image diff --git a/Yep/Views/Feed/FeedView.swift b/Yep/Views/Feed/FeedView.swift index f7a369d0..83a2c03c 100644 --- a/Yep/Views/Feed/FeedView.swift +++ b/Yep/Views/Feed/FeedView.swift @@ -18,7 +18,7 @@ class FeedView: UIView { } } - var tapMediaAction: ((transitionView: UIView, imageURLs: [NSURL], index: Int) -> Void)? + var tapMediaAction: ((transitionView: UIView, attachments: [DiscoveredAttachment], index: Int) -> Void)? static let foldHeight: CGFloat = 60 @@ -33,7 +33,7 @@ class FeedView: UIView { if newValue >= 0 && newValue <= 1 { let normalHeight = self.normalHeight - let attachmentURLsIsEmpty = attachmentURLs.isEmpty + let attachmentURLsIsEmpty = attachments.isEmpty UIView.animateWithDuration(0.3, delay: 0, usingSpringWithDamping: 0.75, initialSpringVelocity: 0.0, options: UIViewAnimationOptions(rawValue: 0), animations: { [weak self] in @@ -100,11 +100,10 @@ class FeedView: UIView { @IBOutlet weak var timeLabel: UILabel! @IBOutlet weak var timeLabelTopConstraint: NSLayoutConstraint! - var attachmentURLs = [NSURL]() { + var attachments = [DiscoveredAttachment]() { didSet { mediaCollectionView.reloadData() - mediaView.FeedURLs = attachmentURLs - mediaView.setImagesWithURLs(attachmentURLs) + mediaView.setImagesWithAttachments(attachments) } } @@ -201,7 +200,9 @@ class FeedView: UIView { timeLabelTopConstraint.constant = hasMedia ? (15 + 80 + 15) : 15 mediaCollectionView.hidden = hasMedia ? false : true - attachmentURLs = feed.attachments.map({ NSURL(string: $0.URLString) }).flatMap({ $0 }) + attachments = feed.attachments.map({ + DiscoveredAttachment(kind: AttachmentKind(rawValue: $0.kind)!, metadata: $0.metadata, URLString: $0.URLString) + })//.map({ NSURL(string: $0.URLString) }).flatMap({ $0 }) if let creator = feed.creator { let userAvatar = UserAvatar(userID: creator.userID, avatarStyle: nanoAvatarStyle) @@ -243,20 +244,18 @@ extension FeedView: UICollectionViewDataSource, UICollectionViewDelegate { } func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { - return attachmentURLs.count + return attachments.count } func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCellWithReuseIdentifier(feedMediaCellID, forIndexPath: indexPath) as! FeedMediaCell - let imageURL = attachmentURLs[indexPath.item] + let attachment = attachments[indexPath.item] //println("attachment imageURL: \(imageURL)") - cell.attachmentURL = imageURL - - cell.configureWithImageURL(imageURL, bigger: (attachmentURLs.count == 1)) + cell.configureWithAttachment(attachment, bigger: (attachments.count == 1)) return cell } @@ -275,7 +274,7 @@ extension FeedView: UICollectionViewDataSource, UICollectionViewDelegate { let cell = collectionView.cellForItemAtIndexPath(indexPath) as! FeedMediaCell let transitionView = cell.imageView - tapMediaAction?(transitionView: transitionView, imageURLs: attachmentURLs, index: indexPath.item) + tapMediaAction?(transitionView: transitionView, attachments: attachments, index: indexPath.item) } } From d167a3cd16eae09929d352f98cacba6837745174 Mon Sep 17 00:00:00 2001 From: nixzhu Date: Mon, 2 Nov 2015 17:58:50 +0800 Subject: [PATCH 3/4] use DiscoveredAttachment's thumbnailImage for FeedMediaView if can --- Yep/Services/YepService.swift | 16 ++ .../FeedConversationCell.swift | 2 +- Yep/Views/Feed/FeedMediaView.swift | 198 +++++++++++------- 3 files changed, 137 insertions(+), 79 deletions(-) diff --git a/Yep/Services/YepService.swift b/Yep/Services/YepService.swift index f742a953..9e02d222 100644 --- a/Yep/Services/YepService.swift +++ b/Yep/Services/YepService.swift @@ -2397,6 +2397,22 @@ struct DiscoveredAttachment { let metadata: String let URLString: String + var thumbnailImage: UIImage? { + + if let data = metadata.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false) { + if let metaDataInfo = decodeJSON(data) { + if let thumbnailString = metaDataInfo[YepConfig.MetaData.thumbnailString] as? String { + if let imageData = NSData(base64EncodedString: thumbnailString, options: NSDataBase64DecodingOptions(rawValue: 0)) { + let image = UIImage(data: imageData) + return image + } + } + } + } + + return nil + } + static func fromJSONDictionary(json: JSONDictionary) -> DiscoveredAttachment? { guard let kindString = json["kind"] as? String, diff --git a/Yep/Views/Cells/FeedConversation/FeedConversationCell.swift b/Yep/Views/Cells/FeedConversation/FeedConversationCell.swift index 4a284b48..c94f956c 100644 --- a/Yep/Views/Cells/FeedConversation/FeedConversationCell.swift +++ b/Yep/Views/Cells/FeedConversation/FeedConversationCell.swift @@ -75,7 +75,7 @@ class FeedConversationCell: UITableViewCell { let attachments = feed.attachments.map({ DiscoveredAttachment(kind: AttachmentKind(rawValue: $0.kind)!, metadata: $0.metadata, URLString: $0.URLString) - })//.map({ NSURL(string: $0.URLString) }).flatMap({ $0 }) + }) mediaView.setImagesWithAttachments(attachments) if let latestMessage = messagesInConversation(conversation).last { diff --git a/Yep/Views/Feed/FeedMediaView.swift b/Yep/Views/Feed/FeedMediaView.swift index 267a79c7..5d2b9931 100644 --- a/Yep/Views/Feed/FeedMediaView.swift +++ b/Yep/Views/Feed/FeedMediaView.swift @@ -52,80 +52,105 @@ class FeedMediaView: UIView { hidden = (attachments.count == 0) -// println(URLs) - attachmentURLs = attachments.map({ NSURL(string: $0.URLString) }).flatMap({ $0 }) switch attachments.count { case 1: imageView1.frame = fullRect -// imageView1.kf_setImageWithURL(URLs[0]) - - ImageCache.sharedInstance.imageOfAttachment(attachments[0], withSize: fullRect.size, completion: { [weak self] (url, image) in - guard let strongSelf = self where strongSelf.attachmentURLs.contains(url) else { - return - } - self?.imageView1.image = image - }) + + if let thumbnailImage = attachments[0].thumbnailImage { + imageView1.image = thumbnailImage + + } else { + ImageCache.sharedInstance.imageOfAttachment(attachments[0], withSize: fullRect.size, completion: { [weak self] (url, image) in + guard let strongSelf = self where strongSelf.attachmentURLs.contains(url) else { + return + } + self?.imageView1.image = image + }) + } addSubview(imageView1) case 2: imageView1.frame = halfRect imageView1.center = CGPoint(x: halfRect.width * 0.5, y: imageView1.center.y) -// imageView1.kf_setImageWithURL(URLs[0]) - ImageCache.sharedInstance.imageOfAttachment(attachments[0], withSize: halfRect.size, completion: { [weak self] (url, image) in - guard let strongSelf = self where strongSelf.attachmentURLs.contains(url) else { - return - } - self?.imageView1.image = image - }) + + if let thumbnailImage = attachments[0].thumbnailImage { + imageView1.image = thumbnailImage + + } else { + ImageCache.sharedInstance.imageOfAttachment(attachments[0], withSize: halfRect.size, completion: { [weak self] (url, image) in + guard let strongSelf = self where strongSelf.attachmentURLs.contains(url) else { + return + } + self?.imageView1.image = image + }) + } imageView2.frame = halfRect imageView2.center = CGPoint(x: halfRect.width * 1.5, y: imageView2.center.y) -// imageView2.kf_setImageWithURL(URLs[1]) - ImageCache.sharedInstance.imageOfAttachment(attachments[1], withSize: halfRect.size, completion: { [weak self] (url, image) in - guard let strongSelf = self where strongSelf.attachmentURLs.contains(url) else { - return - } - self?.imageView2.image = image - }) + + if let thumbnailImage = attachments[1].thumbnailImage { + imageView2.image = thumbnailImage + + } else { + ImageCache.sharedInstance.imageOfAttachment(attachments[1], withSize: halfRect.size, completion: { [weak self] (url, image) in + guard let strongSelf = self where strongSelf.attachmentURLs.contains(url) else { + return + } + self?.imageView2.image = image + }) + } addSubview(imageView1) addSubview(imageView2) case 3: imageView1.frame = quarterRect -// imageView1.kf_setImageWithURL(URLs[0]) - ImageCache.sharedInstance.imageOfAttachment(attachments[0], withSize: quarterRect.size, completion: { [weak self] (url, image) in - guard let strongSelf = self where strongSelf.attachmentURLs.contains(url) else { - return - } - self?.imageView1.image = image - }) + + if let thumbnailImage = attachments[0].thumbnailImage { + imageView1.image = thumbnailImage + + } else { + ImageCache.sharedInstance.imageOfAttachment(attachments[0], withSize: quarterRect.size, completion: { [weak self] (url, image) in + guard let strongSelf = self where strongSelf.attachmentURLs.contains(url) else { + return + } + self?.imageView1.image = image + }) + } imageView2.frame = quarterRect imageView2.center = CGPoint(x: imageView2.center.x, y: quarterRect.height * 1.5) -// imageView2.kf_setImageWithURL(URLs[1]) - - ImageCache.sharedInstance.imageOfAttachment(attachments[1], withSize: quarterRect.size, completion: { [weak self] (url, image) in - guard let strongSelf = self where strongSelf.attachmentURLs.contains(url) else { - return - } - self?.imageView2.image = image - }) + + if let thumbnailImage = attachments[1].thumbnailImage { + imageView2.image = thumbnailImage + + } else { + ImageCache.sharedInstance.imageOfAttachment(attachments[1], withSize: quarterRect.size, completion: { [weak self] (url, image) in + guard let strongSelf = self where strongSelf.attachmentURLs.contains(url) else { + return + } + self?.imageView2.image = image + }) + } imageView3.frame = halfRect imageView3.center = CGPoint(x: halfRect.width * 1.5, y: imageView3.center.y) -// imageView3.kf_setImageWithURL(URLs[2]) - - ImageCache.sharedInstance.imageOfAttachment(attachments[2], withSize: quarterRect.size, completion: { [weak self] (url, image) in - guard let strongSelf = self where strongSelf.attachmentURLs.contains(url) else { - return - } - self?.imageView3.image = image - }) + + if let thumbnailImage = attachments[2].thumbnailImage { + imageView3.image = thumbnailImage + + } else { + ImageCache.sharedInstance.imageOfAttachment(attachments[2], withSize: quarterRect.size, completion: { [weak self] (url, image) in + guard let strongSelf = self where strongSelf.attachmentURLs.contains(url) else { + return + } + self?.imageView3.image = image + }) + } addSubview(imageView1) addSubview(imageView2) @@ -134,46 +159,63 @@ class FeedMediaView: UIView { case 4.. Date: Mon, 2 Nov 2015 18:07:15 +0800 Subject: [PATCH 4/4] better thumbnail --- Yep/Helpers/YepMedia.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Yep/Helpers/YepMedia.swift b/Yep/Helpers/YepMedia.swift index 9aac9598..b57d35cd 100644 --- a/Yep/Helpers/YepMedia.swift +++ b/Yep/Helpers/YepMedia.swift @@ -28,7 +28,7 @@ func metaDataStringOfImage(image: UIImage, needBlurThumbnail: Bool) -> String? { let thumbnailSize = CGSize(width: thumbnailWidth, height: thumbnailHeight) - if let thumbnail = image.resizeToSize(thumbnailSize, withInterpolationQuality: CGInterpolationQuality.High) { + if let thumbnail = image.navi_resizeToSize(thumbnailSize, withInterpolationQuality: CGInterpolationQuality.High) { if needBlurThumbnail {