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