Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
381 views
in Technique[技术] by (71.8m points)

ios - Unable to Return RGB Colors (CGPoint, CGImage)

The program runs but unfortunately I'm unable to figure out why it is not returning RGB. Could anyone possibly tell me what could be wrong? The crosshair (CGPoint) is panning over the image taken from camera and is showing that the gestures are working on the Xcode console but no RGB is being returned successfully.

I am unable to figure out what the issue may be regarding my code:

import UIKit

class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    
    @IBOutlet weak var cameraButton: UIButton!
    @IBOutlet weak var cameraView: UIImageView!
    @IBOutlet weak var photosButton: UIButton!
    @IBOutlet weak var crosshair: UIImageView!
    var cameraView2: CGImage?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        addPanGesture(view: crosshair)
        //crosshair.layer.position = CGPoint(x: 5, y: 5)
        crosshair2 = crosshair.frame.origin
        print("viewDidLoad")
        view.bringSubviewToFront(crosshair)
      }
    
    var crosshair2: CGPoint!
      
      func addPanGesture(view: UIImageView) {
          let pan = UIPanGestureRecognizer(target: self, action: #selector(ViewController.handlePan(sender:)))
          view.addGestureRecognizer(pan)
          print("addPanGesture")
      }
      
      // Refactor
      @objc func handlePan(sender: UIPanGestureRecognizer) {
          print("handlePan")
          let fileView = sender.view!
          switch sender.state {
          case .began, .changed:
              moveViewWithPan(view: fileView, sender: sender)
          default:
              break
          }
      }
      
      func moveViewWithPan(view: UIView, sender: UIPanGestureRecognizer) {
          let translation = sender.translation(in: view)
          view.center = CGPoint(x: view.center.x + translation.x, y: view.center.y + translation.y)
          sender.setTranslation(CGPoint.zero, in: view)
          print("moveViewWithPan")
      }
    
    @IBAction func tappedCameraButton(_ sender: Any) {
        let cam = UIImagePickerController()
        cam.sourceType = .camera
        cam.allowsEditing = true
        cam.delegate = self
        present(cam, animated: true)
        print("tappedCameraButton")
    }

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        cameraView?.image = info[UIImagePickerController.InfoKey.editedImage] as? UIImage
        picker.dismiss(animated: true, completion: nil)
        print("imagePickerController")
    }
    
//extension UIImageView {
    func getPixelColor(pos: CGPoint) -> UIColor? {
        cameraView2 = cameraView.image?.cgImage
        guard let cgImage = cameraView2, let provider = cgImage.dataProvider
        else {
            return nil
        }
        let pixelData = provider.data
        let data: UnsafePointer<UInt8> = CFDataGetBytePtr(pixelData)
        let screenWidth  = UIScreen.main.fixedCoordinateSpace.bounds.width
        let pixelInfo: Int = ((Int (screenWidth) * Int(pos.y)) + Int(pos.x)) * 4
        
        let r = CGFloat(data[pixelInfo]) / CGFloat(255.0)
        let g = CGFloat(data[pixelInfo+1]) / CGFloat(255.0)
        let b = CGFloat(data[pixelInfo+2]) / CGFloat(255.0)
        let a = CGFloat(data[pixelInfo+3]) / CGFloat(255.0)
        let color = UIColor(red: r, green: g, blue: b, alpha: a)
                return(color)
    }
}

It runs but displays an error in the console when I open up the camera saying:

2021-01-13 21:01:25.113734-0500 RGB Returner[17055:4324721] [Camera] Failed to read exposureBiasesByMode dictionary: Error Domain=NSCocoaErrorDomain Code=4864 "*** -[NSKeyedUnarchiver _initForReadingFromData:error:throwLegacyExceptions:]: data is NULL" UserInfo={NSDebugDescription=*** -[NSKeyedUnarchiver _initForReadingFromData:error:throwLegacyExceptions:]: data is NULL}
question from:https://stackoverflow.com/questions/65713118/unable-to-return-rgb-colors-cgpoint-cgimage

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

Please log in or register to answer this question.

Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...