Guest User

Untitled

a guest
Dec 18th, 2018
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.64 KB | None | 0 0
  1. if let firstImageData = UIImageJPEGRepresentation(pickedImage, 0.1) {
  2. self.imgArray.append(firstImageData)
  3. }
  4.  
  5. extension UIImage {
  6. func resizeWithPercent(percentage: CGFloat) -> UIImage? {
  7. let imageView = UIImageView(frame: CGRect(origin: .zero, size: CGSize(width: size.width * percentage, height: size.height * percentage)))
  8. imageView.contentMode = .ScaleAspectFit
  9. imageView.image = self
  10. UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, false, scale)
  11. guard let context = UIGraphicsGetCurrentContext() else { return nil }
  12. imageView.layer.renderInContext(context)
  13. guard let result = UIGraphicsGetImageFromCurrentImageContext() else { return nil }
  14. UIGraphicsEndImageContext()
  15. return result
  16. }
  17. func resizeWithWidth(width: CGFloat) -> UIImage? {
  18. let imageView = UIImageView(frame: CGRect(origin: .zero, size: CGSize(width: width, height: CGFloat(ceil(width/size.width * size.height)))))
  19. imageView.contentMode = .ScaleAspectFit
  20. imageView.image = self
  21. UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, false, scale)
  22. guard let context = UIGraphicsGetCurrentContext() else { return nil }
  23. imageView.layer.renderInContext(context)
  24. guard let result = UIGraphicsGetImageFromCurrentImageContext() else { return nil }
  25. UIGraphicsEndImageContext()
  26. return result
  27. }
  28. }
  29.  
  30. myImage = myImage.resizeWithWidth(700)!
  31.  
  32. let compressData = UIImageJPEGRepresentation(myImage, 0.5) //max value is 1.0 and minimum is 0.0
  33. let compressedImage = UIImage(data: compressData!)
Add Comment
Please, Sign In to add comment