Advertisement
Guest User

Untitled

a guest
May 21st, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.94 KB | None | 0 0
  1. func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
  2. var img : UIImage!
  3. var originalImage: UIImage!
  4. if let image = info[UIImagePickerControllerEditedImage] as? UIImage {
  5. img = image.compressTo(500)
  6. originalImage = image
  7. }
  8. else if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
  9. img = image.compressTo(500)
  10. originalImage = image
  11. }
  12.  
  13. // if picker == imagePicker {
  14. // picture = img
  15. // }
  16.  
  17. let cropController = CropViewController(croppingStyle: .default, image: originalImage)
  18. cropController.delegate = self
  19. cropController.aspectRatioPreset = .presetCustom
  20. cropController.customAspectRatio = CGSize(width: 1.0, height: 1.12)
  21. cropController.aspectRatioLockEnabled = true
  22. cropController.aspectRatioPickerButtonHidden = true
  23. cropController.resetButtonHidden = false
  24. cropController.rotateButtonsHidden = true
  25. cropController.resetAspectRatioEnabled = false
  26.  
  27. dismiss(animated: true, completion: { [weak self] in
  28. guard let strongSelf = self else { return }
  29. strongSelf.present(cropController, animated: true, completion: nil)
  30. })
  31. }
  32.  
  33. func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {
  34. captureSession?.stopRunning()
  35.  
  36.  
  37. if let imageData = photo.fileDataRepresentation() {
  38. image = UIImage(data: imageData)?.compressTo(500)
  39. let outputRect = videoPreviewLayer?.metadataOutputRectConverted(fromLayerRect: videoPreviewLayer?.bounds ?? .zero) ?? .zero
  40. let takenCGImage = image?.cgImage
  41. let width = takenCGImage?.width ?? 0
  42. let height = takenCGImage?.height ?? 0
  43. let cropRect = CGRect(x: outputRect.origin.x * CGFloat(width), y: outputRect.origin.y * CGFloat(height), width: outputRect.size.width * CGFloat(width), height: outputRect.size.height * CGFloat(height))
  44. let cropCGImage = takenCGImage?.cropping(to: cropRect)
  45.  
  46. if currentCameraPosition == .front {
  47. if let cropCGImage = cropCGImage {
  48. image = UIImage(cgImage: cropCGImage, scale: 1.0, orientation: .leftMirrored)
  49. }
  50. } else {
  51. if let cropCGImage = cropCGImage, let imageOrientation = image?.imageOrientation {
  52. image = UIImage(cgImage: cropCGImage, scale: 1, orientation: imageOrientation)
  53. }
  54. }
  55. picture = image
  56. let previewImageVC = PreviewImageVC.instantiate(from: .main)
  57. previewImageVC.pickedImage = image
  58. previewImageVC.navigationBarButtonsTapped = { [weak self] in
  59. guard let strongSelf = self else { return }
  60. strongSelf.mainView.captureImageButton.isEnabled = true
  61. }
  62. navigationController?.pushViewController(previewImageVC, animated: true)
  63. }
  64. }
  65.  
  66. func compressTo(_ expectedSizeInKb:CGFloat) -> UIImage? {
  67. let sizeInBytes = expectedSizeInKb * 1024
  68. var needCompress:Bool = true
  69. var imgData:Data?
  70. var compressingValue:CGFloat = 1.0
  71. while (needCompress && compressingValue > 0.0) {
  72. if let data:Data = UIImageJPEGRepresentation(self, compressingValue) {
  73. if CGFloat(data.count) < sizeInBytes {
  74. needCompress = false
  75. imgData = data
  76. } else {
  77. compressingValue -= 0.1
  78. }
  79. }
  80. }
  81.  
  82. if let data = imgData {
  83. if (CGFloat(data.count) < sizeInBytes) {
  84. return UIImage(data: data)
  85. }
  86. }
  87. return nil
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement