marinofaggiana 6 年之前
父节点
当前提交
bb4da82436
共有 2 个文件被更改,包括 66 次插入5 次删除
  1. 13 2
      iOSClient/Imagemeter/IMImagemeter.swift
  2. 53 3
      iOSClient/Viewer/NCViewerImagemeter.swift

+ 13 - 2
iOSClient/Imagemeter/IMImagemeter.swift

@@ -141,8 +141,8 @@ class IMImagemeterCodable: NSObject {
         
         struct thumbnails: Codable {
             let filename: String
-            let width: Int
-            let height: Int
+            let width: Double
+            let height: Double
         }
         
         let is_example_image: Bool
@@ -188,4 +188,15 @@ class IMImagemeterCodable: NSObject {
             return nil
         }
     }
+    
+    func convertCoordinate(x: Double, y: Double, width: Double, height: Double, button: Double) -> (x: Double, y: Double) {
+        
+        let normalizeX: Double = floor(512 + x)
+        let normalizeY: Double = floor(384 + y)
+        
+        let factorX: Double = width * normalizeX / 1024
+        let factorY: Double = height * normalizeY / 768
+        
+        return(factorX, factorY)
+    }
 }

+ 53 - 3
iOSClient/Viewer/NCViewerImagemeter.swift

@@ -23,15 +23,20 @@
 
 import Foundation
 
-class NCViewerImagemeter: UIViewController {
+class NCViewerImagemeter: UIViewController, AVAudioPlayerDelegate {
     
     @IBOutlet weak var img: UIImageView!
     @IBOutlet weak var imgHeightConstraint: NSLayoutConstraint!
     
     private let appDelegate = UIApplication.shared.delegate as! AppDelegate
     private var nameArchiveImagemeter: String = ""
+    
     private var pathArchiveImagemeter: String = ""
+    
     private var annotation: IMImagemeterCodable.imagemeterAnnotation?
+  
+    private var audioPlayer = AVAudioPlayer()
+
     var metadata: tableMetadata?
     
 
@@ -62,6 +67,7 @@ class NCViewerImagemeter: UIViewController {
                 
                 self.annotation = annotation
                 imgThumbnails()
+                imgAudio()
                 
             } else {
                 appDelegate.messageNotification("_error_", description: "_error_decompressing_", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: Int(k_CCErrorInternalError))
@@ -82,16 +88,60 @@ class NCViewerImagemeter: UIViewController {
             if let thumbnailsWidth = annotation.thumbnails.first?.width {
                 if let thumbnailsHeight = annotation.thumbnails.first?.height {
                     
-                    let ratio = Float(thumbnailsWidth) / Float(thumbnailsHeight)
+                    let factor = Float(thumbnailsWidth) / Float(thumbnailsHeight)
                     let imageWidth = self.view.bounds.size.width
                     
-                    imgHeightConstraint.constant = CGFloat((Float(imageWidth) / ratio))
+                    imgHeightConstraint.constant = CGFloat((Float(imageWidth) / factor))
                     img.image = UIImage(contentsOfFile: pathArchiveImagemeter + "/" + thumbnailsFilename)
                 }
             }
         }
     }
     
+    func imgAudio() {
+        
+        guard let annotation = self.annotation else {
+            return
+        }
+        
+        for element in annotation.elements {
+            
+            let coordinateNormalize =  IMImagemeterCodable.sharedInstance.convertCoordinate(x: element.center.x, y: element.center.y, width: Double(self.view.bounds.width), height: Double(imgHeightConstraint.constant), button: 30)
+            let x = coordinateNormalize.x
+            let y = coordinateNormalize.y + 15
+            
+            let button = UIButton()
+            button.frame = CGRect(x: x, y: y, width: 30, height: 30)
+            button.setImage(UIImage(named: "audioPlay"), for: .normal)
+            button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
+            button.tag = element.id
+    
+            self.view.addSubview(button)
+        }
+    }
+    
+    @objc func buttonAction(sender: UIButton!) {
+        
+        guard let annotation = self.annotation else {
+            return
+        }
+        
+        for element in annotation.elements {
+            if element.id == sender.tag {
+                let fileNamePath =  pathArchiveImagemeter + "/" + element.audio_recording.recording_filename
+                // player
+                do {
+                    try audioPlayer = AVAudioPlayer(contentsOf: URL(fileURLWithPath: fileNamePath))
+                    audioPlayer.delegate = self
+                    audioPlayer.prepareToPlay()
+                    audioPlayer.play()
+                } catch {
+                }
+                
+            }
+        }
+    }
+    
     @objc func close() {
         self.dismiss(animated: true, completion: nil)
     }