Created
May 17, 2018 12:34
-
-
Save mspvirajpatel/1d4c6ad3e4e51b2082da2fc1bac6a108 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension UIImage { | |
enum CompressImageErrors: Error { | |
case invalidExSize | |
case sizeImpossibleToReach | |
} | |
func compressImage(_ expectedSizeKb: Int, completion : (UIImage,CGFloat) -> Void ) throws { | |
let minimalCompressRate :CGFloat = 0.4 // min compressRate to be checked later | |
if expectedSizeKb == 0 { | |
throw CompressImageErrors.invalidExSize // if the size is equal to zero throws | |
} | |
let expectedSizeBytes = expectedSizeKb * 1024 | |
let imageToBeHandled: UIImage = self | |
var actualHeight : CGFloat = self.size.height | |
var actualWidth : CGFloat = self.size.width | |
var maxHeight : CGFloat = self.size.height //A4 default size I'm thinking about a document | |
var maxWidth : CGFloat = self.size.width | |
var imgRatio : CGFloat = actualWidth/actualHeight | |
let maxRatio : CGFloat = maxWidth/maxHeight | |
var compressionQuality : CGFloat = 1 | |
var imageData:Data = UIImageJPEGRepresentation(imageToBeHandled, compressionQuality)! | |
while imageData.count > expectedSizeBytes { | |
if (actualHeight > maxHeight || actualWidth > maxWidth){ | |
if(imgRatio < maxRatio){ | |
imgRatio = maxHeight / actualHeight; | |
actualWidth = imgRatio * actualWidth; | |
actualHeight = maxHeight; | |
} | |
else if(imgRatio > maxRatio){ | |
imgRatio = maxWidth / actualWidth; | |
actualHeight = imgRatio * actualHeight; | |
actualWidth = maxWidth; | |
} | |
else{ | |
actualHeight = maxHeight; | |
actualWidth = maxWidth; | |
compressionQuality = 1; | |
} | |
} | |
let rect = CGRect(x: 0.0, y: 0.0, width: actualWidth, height: actualHeight) | |
UIGraphicsBeginImageContext(rect.size); | |
imageToBeHandled.draw(in: rect) | |
let img = UIGraphicsGetImageFromCurrentImageContext(); | |
UIGraphicsEndImageContext(); | |
if let imgData = UIImageJPEGRepresentation(img!, compressionQuality) { | |
if imgData.count > expectedSizeBytes { | |
if compressionQuality > minimalCompressRate { | |
compressionQuality -= 0.1 | |
} else { | |
maxHeight = maxHeight * 0.9 | |
maxWidth = maxWidth * 0.9 | |
} | |
} | |
imageData = imgData | |
} | |
} | |
completion(UIImage(data: imageData)!, compressionQuality) | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
do { | |
try img.compressImage(300, completion: { [weak self] (image, compressRatio) in | |
mainPro.uploadBackgroundStoryImage(img: image, progressBlock: { [weak self] (progress) in | |
DispatchQueue.main.async { | |
print(progress) | |
if let firstCell = self!.storyCollectionView.cellForItem(at: IndexPath.init(row: 0, section: 0)) as? StoryCollectionViewCell { | |
firstCell.progressView.updateProgress(CGFloat(progress / 100)) | |
} | |
} | |
}, callBack: { [weak self] (complated, url, error, uploadtask) in | |
DispatchQueue.main.async { | |
self!.createStory(url: url!, type: "image", thumb: nil, duration: "5.0") | |
} | |
}, token: { (lastToken) -> Void? in | |
DispatchQueue.global(qos: .background).async { | |
StoryUploadManager.sharedManager.startStoryUploading(isResume: false) | |
} | |
return nil | |
}, failedBlock: { [weak self] (error) in | |
DispatchQueue.main.async { | |
if let err = error { | |
self!.showAlert(alertMessage: err.localizedDescription) | |
if let firstCell = self!.storyCollectionView.cellForItem(at: IndexPath.init(row: 0, section: 0)) as? StoryCollectionViewCell { | |
firstCell.progressView.updateProgress(0.0) | |
} | |
} | |
} | |
}) | |
}) | |
} catch { | |
print("Error") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment