Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
- var img : UIImage!
- var originalImage: UIImage!
- if let image = info[UIImagePickerControllerEditedImage] as? UIImage {
- img = image.compressTo(500)
- originalImage = image
- }
- else if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
- img = image.compressTo(500)
- originalImage = image
- }
- // if picker == imagePicker {
- // picture = img
- // }
- let cropController = CropViewController(croppingStyle: .default, image: originalImage)
- cropController.delegate = self
- cropController.aspectRatioPreset = .presetCustom
- cropController.customAspectRatio = CGSize(width: 1.0, height: 1.12)
- cropController.aspectRatioLockEnabled = true
- cropController.aspectRatioPickerButtonHidden = true
- cropController.resetButtonHidden = false
- cropController.rotateButtonsHidden = true
- cropController.resetAspectRatioEnabled = false
- dismiss(animated: true, completion: { [weak self] in
- guard let strongSelf = self else { return }
- strongSelf.present(cropController, animated: true, completion: nil)
- })
- }
- func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {
- captureSession?.stopRunning()
- if let imageData = photo.fileDataRepresentation() {
- image = UIImage(data: imageData)?.compressTo(500)
- let outputRect = videoPreviewLayer?.metadataOutputRectConverted(fromLayerRect: videoPreviewLayer?.bounds ?? .zero) ?? .zero
- let takenCGImage = image?.cgImage
- let width = takenCGImage?.width ?? 0
- let height = takenCGImage?.height ?? 0
- 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))
- let cropCGImage = takenCGImage?.cropping(to: cropRect)
- if currentCameraPosition == .front {
- if let cropCGImage = cropCGImage {
- image = UIImage(cgImage: cropCGImage, scale: 1.0, orientation: .leftMirrored)
- }
- } else {
- if let cropCGImage = cropCGImage, let imageOrientation = image?.imageOrientation {
- image = UIImage(cgImage: cropCGImage, scale: 1, orientation: imageOrientation)
- }
- }
- picture = image
- let previewImageVC = PreviewImageVC.instantiate(from: .main)
- previewImageVC.pickedImage = image
- previewImageVC.navigationBarButtonsTapped = { [weak self] in
- guard let strongSelf = self else { return }
- strongSelf.mainView.captureImageButton.isEnabled = true
- }
- navigationController?.pushViewController(previewImageVC, animated: true)
- }
- }
- func compressTo(_ expectedSizeInKb:CGFloat) -> UIImage? {
- let sizeInBytes = expectedSizeInKb * 1024
- var needCompress:Bool = true
- var imgData:Data?
- var compressingValue:CGFloat = 1.0
- while (needCompress && compressingValue > 0.0) {
- if let data:Data = UIImageJPEGRepresentation(self, compressingValue) {
- if CGFloat(data.count) < sizeInBytes {
- needCompress = false
- imgData = data
- } else {
- compressingValue -= 0.1
- }
- }
- }
- if let data = imgData {
- if (CGFloat(data.count) < sizeInBytes) {
- return UIImage(data: data)
- }
- }
- return nil
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement