Skip to content

Commit

Permalink
Merge branch 'feature/progressive_image_download' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
nixzhu committed Jan 30, 2016
2 parents fa2d688 + 35c13e1 commit 051f339
Show file tree
Hide file tree
Showing 8 changed files with 260 additions and 17 deletions.
4 changes: 4 additions & 0 deletions Yep.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@
50AFBF4B1BB2A44C007D19DA /* InfoView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 50AFBF4A1BB2A44C007D19DA /* InfoView.swift */; };
50AFBF4E1BB3A594007D19DA /* bub3.caf in Resources */ = {isa = PBXBuildFile; fileRef = 50AFBF4D1BB3A594007D19DA /* bub3.caf */; };
50AFBF5F1BB4F332007D19DA /* FeedsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 50AFBF5E1BB4F332007D19DA /* FeedsViewController.swift */; };
50B22E461C588B6200219625 /* CGImage+Yep.swift in Sources */ = {isa = PBXBuildFile; fileRef = 50B22E451C588B6200219625 /* CGImage+Yep.swift */; };
50B2CA921BD0BF3800668AC6 /* FeedTextView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 50B2CA911BD0BF3800668AC6 /* FeedTextView.swift */; };
50B2CA941BD0EF7900668AC6 /* MoreMessageTypesView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 50B2CA931BD0EF7900668AC6 /* MoreMessageTypesView.swift */; };
50B38FB71B69CD960064C132 /* Array+Yep.swift in Sources */ = {isa = PBXBuildFile; fileRef = 50B38FB61B69CD960064C132 /* Array+Yep.swift */; };
Expand Down Expand Up @@ -619,6 +620,7 @@
50AFBF4A1BB2A44C007D19DA /* InfoView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = InfoView.swift; path = Views/InfoView/InfoView.swift; sourceTree = "<group>"; };
50AFBF4D1BB3A594007D19DA /* bub3.caf */ = {isa = PBXFileReference; lastKnownFileType = file; name = bub3.caf; path = Sounds/bub3.caf; sourceTree = "<group>"; };
50AFBF5E1BB4F332007D19DA /* FeedsViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = FeedsViewController.swift; path = ViewControllers/Feeds/FeedsViewController.swift; sourceTree = "<group>"; };
50B22E451C588B6200219625 /* CGImage+Yep.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = "CGImage+Yep.swift"; path = "Extensions/CGImage+Yep.swift"; sourceTree = "<group>"; };
50B2CA911BD0BF3800668AC6 /* FeedTextView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = FeedTextView.swift; path = Views/TextViews/FeedTextView.swift; sourceTree = "<group>"; };
50B2CA931BD0EF7900668AC6 /* MoreMessageTypesView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = MoreMessageTypesView.swift; path = Views/MoreMessageTypes/MoreMessageTypesView.swift; sourceTree = "<group>"; };
50B38FB61B69CD960064C132 /* Array+Yep.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = "Array+Yep.swift"; path = "Extensions/Array+Yep.swift"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1137,6 +1139,7 @@
50DBDF041C4DD9530044E21B /* CGSize+Yep.swift */,
5023DE511AB6C51300B3EE96 /* CGRect+Yep.swift */,
5023DE581AB6DBC100B3EE96 /* UIImage+Yep.swift */,
50B22E451C588B6200219625 /* CGImage+Yep.swift */,
506EC2091AC95BFC00CE42B3 /* UIFont+Yep.swift */,
0A3E605F1ACCA9C0003DC853 /* String+Yep.swift */,
50A9D44A1ACD37A8000B2599 /* NSFileManager+Yep.swift */,
Expand Down Expand Up @@ -2649,6 +2652,7 @@
50CDBE421ACA9B4200459CE0 /* ChatRightImageCell.swift in Sources */,
5099B9C41AB9510D002F940B /* RankView.swift in Sources */,
502AE5311AB85067005BD199 /* RegisterPickMobileViewController.swift in Sources */,
50B22E461C588B6200219625 /* CGImage+Yep.swift in Sources */,
50535D1A1BCD19AE002FE1A4 /* TouchClosuresView.swift in Sources */,
506E241E1AE6391A00A33C1A /* TouchZoomButton.swift in Sources */,
5020B8A51C059662006CE8EA /* VoiceRecordSampleView.swift in Sources */,
Expand Down
7 changes: 5 additions & 2 deletions Yep/Caches/ImageCache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -213,11 +213,14 @@ class ImageCache {

let mediaType = message.mediaType

YepDownloader.downloadAttachmentsOfMessage(message, reportProgress: { progress in
YepDownloader.downloadAttachmentsOfMessage(message, reportProgress: { progress, image in
dispatch_async(dispatch_get_main_queue()) {
completion(loadingProgress: progress, image: nil)
completion(loadingProgress: progress, image: image)
}

}, imageTransform: { image in
return image.bubbleImageWithTailDirection(tailDirection, size: size).decodedImage()

}, imageFinished: { image in

let messageImage = image.bubbleImageWithTailDirection(tailDirection, size: size).decodedImage()
Expand Down
52 changes: 52 additions & 0 deletions Yep/Extensions/CGImage+Yep.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//
// CGImage+Yep.swift
// Yep
//
// Created by nixzhu on 16/1/27.
// Copyright © 2016年 Catch Inc. All rights reserved.
//

import Foundation

extension CGImage {

var yep_extendedCanvasCGImage: CGImage {

let width = CGImageGetWidth(self)
let height = CGImageGetHeight(self)

guard width > 0 && height > 0 else {
return self
}

func hasAlpha() -> Bool {
let alpha = CGImageGetAlphaInfo(self)
switch alpha {
case .First, .Last, .PremultipliedFirst, .PremultipliedLast:
return true
default:
return false
}
}

var bitmapInfo = CGBitmapInfo.ByteOrder32Little.rawValue
if hasAlpha() {
bitmapInfo |= CGImageAlphaInfo.PremultipliedFirst.rawValue
} else {
bitmapInfo |= CGImageAlphaInfo.NoneSkipFirst.rawValue
}

guard let context = CGBitmapContextCreate(nil, width, height, 8, 0, CGColorSpaceCreateDeviceRGB(), bitmapInfo) else {
return self
}

CGContextDrawImage(context, CGRect(x: 0, y: 0, width: width, height: height), self)

guard let newCGImage = CGBitmapContextCreateImage(context) else {
return self
}

return newCGImage
}
}

48 changes: 48 additions & 0 deletions Yep/Extensions/UIImage+Yep.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

import UIKit
import Ruler
import ImageIO
import MobileCoreServices

extension UIImage {

Expand Down Expand Up @@ -572,3 +574,49 @@ extension UIImage {
}
}

// MARK: Progressive

extension UIImage {

var yep_progressiveImage: UIImage? {

guard let cgImage = CGImage else {
return nil
}

let data = NSMutableData()

guard let distination = CGImageDestinationCreateWithData(data, kUTTypeJPEG, 1, nil) else {
return nil
}

let jfifProperties = [
kCGImagePropertyJFIFIsProgressive as String: kCFBooleanTrue as Bool,
kCGImagePropertyJFIFXDensity as String: 72,
kCGImagePropertyJFIFYDensity as String: 72,
kCGImagePropertyJFIFDensityUnit as String: 1,
]

let properties = [
kCGImageDestinationLossyCompressionQuality as String: 0.9,
kCGImagePropertyJFIFDictionary as String: jfifProperties,
]

CGImageDestinationAddImage(distination, cgImage, properties)

guard CGImageDestinationFinalize(distination) else {
return nil
}

guard data.length > 0 else {
return nil
}

guard let progressiveImage = UIImage(data: data) else {
return nil
}

return progressiveImage
}
}

Loading

0 comments on commit 051f339

Please sign in to comment.