Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // RekamWajahViewController.swift
- // SFS-ios
- //
- // Created by UPATIK Macbook on 01/10/24.
- // Copyright © 2024 Universitas Jember. All rights reserved.
- //
- import UIKit
- import AVFoundation
- class RekamWajahViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
- @IBOutlet weak var imageView: UIImageView!
- override func viewDidLoad() {
- super.viewDidLoad()
- }
- func showCameraAccessDeniedAlert() {
- let alert = UIAlertController(
- title: "Akses Kamera Ditolak",
- message: "Aplikasi memerlukan akses kamera. Silakan berikan izin di Pengaturan.",
- preferredStyle: .alert
- )
- alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
- present(alert, animated: true, completion: nil)
- }
- func checkCameraPermission(completion: @escaping (Bool) -> Void) {
- let status = AVCaptureDevice.authorizationStatus(for: .video)
- switch status {
- case .authorized:
- completion(true)
- case .notDetermined:
- AVCaptureDevice.requestAccess(for: .video) { granted in
- completion(granted)
- }
- case .denied, .restricted:
- completion(false)
- @unknown default:
- completion(false)
- }
- }
- @IBAction func takePhoto(_ sender: Any) {
- checkCameraPermission { granted in
- if granted {
- DispatchQueue.main.async {
- let cameraVC = CameraViewController()
- cameraVC.modalPresentationStyle = .fullScreen
- self.present(cameraVC, animated: true, completion: nil)
- }
- } else {
- self.showCameraAccessDeniedAlert()
- }
- }
- }
- func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
- if let originalImage = info[.originalImage] as? UIImage {
- // Crop image to 1:1 ratio and resize to 512x512
- let fixedOrientationImage = fixImageOrientation(image: originalImage)
- let croppedImage = cropToSquare(image: fixedOrientationImage)
- let resizedImage = resizeImage(image: croppedImage, targetSize: CGSize(width: 512, height: 512))
- imageView.image = resizedImage
- }
- dismiss(animated: true, completion: nil)
- }
- func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
- dismiss(animated: true, completion: nil)
- }
- func cropToSquare(image: UIImage) -> UIImage {
- let originalWidth = image.size.width
- let originalHeight = image.size.height
- let cropSize = min(originalWidth, originalHeight)
- let xOffset = (originalWidth - cropSize) / 2
- let yOffset = (originalHeight - cropSize) / 2
- let cropRect = CGRect(x: xOffset, y: yOffset, width: cropSize, height: cropSize)
- let imageRef = image.cgImage?.cropping(to: cropRect)
- return UIImage(cgImage: imageRef!)
- }
- func resizeImage(image: UIImage, targetSize: CGSize) -> UIImage {
- let size = image.size
- let widthRatio = targetSize.width / size.width
- let heightRatio = targetSize.height / size.height
- let scaleFactor = min(widthRatio, heightRatio)
- let newSize = CGSize(width: size.width * scaleFactor, height: size.height * scaleFactor)
- print("ukuran gambar : \(newSize)")
- UIGraphicsBeginImageContextWithOptions(targetSize, false, 0.0)
- image.draw(in: CGRect(origin: .zero, size: targetSize))
- let newImage = UIGraphicsGetImageFromCurrentImageContext()
- UIGraphicsEndImageContext()
- return newImage!
- }
- func fixImageOrientation(image: UIImage) -> UIImage {
- if image.imageOrientation == .up {
- return image
- }
- var transform = CGAffineTransform.identity
- switch image.imageOrientation {
- case .down, .downMirrored:
- transform = transform.translatedBy(x: image.size.width, y: image.size.height)
- transform = transform.rotated(by: .pi)
- case .left, .leftMirrored:
- transform = transform.translatedBy(x: image.size.width, y: 0)
- transform = transform.rotated(by: .pi / 2)
- case .right, .rightMirrored:
- transform = transform.translatedBy(x: 0, y: image.size.height)
- transform = transform.rotated(by: -.pi / 2)
- default:
- break
- }
- switch image.imageOrientation {
- case .upMirrored, .downMirrored:
- transform = transform.translatedBy(x: image.size.width, y: 0)
- transform = transform.scaledBy(x: -1, y: 1)
- case .leftMirrored, .rightMirrored:
- transform = transform.translatedBy(x: image.size.height, y: 0)
- transform = transform.scaledBy(x: -1, y: 1)
- default:
- break
- }
- guard let cgImage = image.cgImage else { return image }
- guard let context = CGContext(
- data: nil,
- width: Int(image.size.width),
- height: Int(image.size.height),
- bitsPerComponent: cgImage.bitsPerComponent,
- bytesPerRow: 0,
- space: cgImage.colorSpace!,
- bitmapInfo: cgImage.bitmapInfo.rawValue
- ) else { return image }
- context.concatenate(transform)
- switch image.imageOrientation {
- case .left, .leftMirrored, .right, .rightMirrored:
- context.draw(cgImage, in: CGRect(x: 0, y: 0, width: image.size.height, height: image.size.width))
- default:
- context.draw(cgImage, in: CGRect(x: 0, y: 0, width: image.size.width, height: image.size.height))
- }
- guard let newCgImage = context.makeImage() else { return image }
- return UIImage(cgImage: newCgImage)
- }
- }
Add Comment
Please, Sign In to add comment