Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Screenshot preview #48

Closed
wants to merge 13 commits into from
Closed
2 changes: 1 addition & 1 deletion YOLO/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<key>CFBundleShortVersionString</key>
<string>$(MARKETING_VERSION)</string>
<key>CFBundleVersion</key>
<string>25</string>
<string>26</string>
<key>ITSAppUsesNonExemptEncryption</key>
<false/>
<key>LSRequiresIPhoneOS</key>
Expand Down
74 changes: 62 additions & 12 deletions YOLO/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class ViewController: UIViewController {
@IBOutlet weak var labelVersion: UILabel!
@IBOutlet weak var labelSlider: UILabel!
@IBOutlet weak var labelSliderConf: UILabel!
var screenshotImageView: UIImageView?
@IBOutlet weak var labelSliderConfLandScape: UILabel!
@IBOutlet weak var labelSliderIoU: UILabel!
@IBOutlet weak var labelSliderIoULandScape: UILabel!
Expand Down Expand Up @@ -403,7 +404,6 @@ class ViewController: UIViewController {
print("no file written")
}
}

// Reading
// do {let text2 = try String(contentsOf: fileURL, encoding: .utf8)} catch {/* error handling here */}
}
Expand Down Expand Up @@ -614,6 +614,46 @@ class ViewController: UIViewController {
default: break
}
} // Pinch to Zoom End --------------------------------------------------------------------------------------------

func showShareAlert(image: UIImage) {
let alertController = UIAlertController(
title: "Do you want to share this image?", message: nil, preferredStyle: .alert)

let shareAction = UIAlertAction(title: "OK", style: .default) { _ in
self.shareImage(image: image)
}

let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { _ in
self.hideScreenshotImageView()
}

alertController.addAction(shareAction)
alertController.addAction(cancelAction)

if let popoverController = alertController.popoverPresentationController {
popoverController.sourceView = self.view
popoverController.sourceRect = CGRect(
x: self.view.bounds.midX, y: self.view.bounds.maxY - 100, width: 0, height: 0)
popoverController.permittedArrowDirections = []
}

present(alertController, animated: true, completion: nil)
}

func shareImage(image: UIImage) {
let activityViewController = UIActivityViewController(
activityItems: [image], applicationActivities: nil)
activityViewController.popoverPresentationController?.sourceView = self.View0
self.present(activityViewController, animated: true) {
self.hideScreenshotImageView()
}
}

func hideScreenshotImageView() {
self.screenshotImageView?.removeFromSuperview()
self.screenshotImageView = nil
}
// ------------------------------------------------------------------------------------------
} // ViewController class End

extension ViewController: VideoCaptureDelegate {
Expand All @@ -635,7 +675,7 @@ extension ViewController: AVCapturePhotoCaptureDelegate {
let cgImageRef: CGImage! = CGImage(
jpegDataProviderSource: dataProvider!, decode: nil, shouldInterpolate: true,
intent: .defaultIntent)
var orientation = CGImagePropertyOrientation.right
var orientation = UIImage.Orientation.right
switch UIDevice.current.orientation {
case .landscapeLeft:
orientation = .up
Expand All @@ -644,16 +684,13 @@ extension ViewController: AVCapturePhotoCaptureDelegate {
default:
break
}
var image = UIImage(cgImage: cgImageRef, scale: 0.5, orientation: .right)
if let orientedCIImage = CIImage(image: image)?.oriented(orientation),
let cgImage = CIContext().createCGImage(orientedCIImage, from: orientedCIImage.extent)
{
image = UIImage(cgImage: cgImage)
}
var image = UIImage(cgImage: cgImageRef, scale: 0.5, orientation: orientation)
let imageView = UIImageView(image: image)
imageView.contentMode = .scaleAspectFill
imageView.frame = videoPreview.frame
let imageLayer = imageView.layer
var sublayers = videoPreview.layer.sublayers ?? []
let insertIndex = max(sublayers.count - 1, 0)
videoPreview.layer.insertSublayer(imageLayer, above: videoCapture.previewLayer)

let bounds = UIScreen.main.bounds
Expand All @@ -662,10 +699,23 @@ extension ViewController: AVCapturePhotoCaptureDelegate {
let img = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
imageLayer.removeFromSuperlayer()
let activityViewController = UIActivityViewController(
activityItems: [img!], applicationActivities: nil)
activityViewController.popoverPresentationController?.sourceView = self.View0
self.present(activityViewController, animated: true, completion: nil)

let screenshotImageView = UIImageView(image: img)
screenshotImageView.frame = view.bounds
screenshotImageView.contentMode = .scaleAspectFit
view.addSubview(screenshotImageView)
self.screenshotImageView = screenshotImageView

UIView.animate(
withDuration: 0.3,
animations: {
screenshotImageView.frame = CGRect(
x: 20, y: 100, width: self.view.bounds.width - 40, height: self.view.bounds.height - 200
)
}
) { _ in
self.showShareAlert(image: img!)
}
//
// // Save to camera roll
// UIImageWriteToSavedPhotosAlbum(img!, nil, nil, nil);
Expand Down
Loading