1. 程式人生 > >iOS 讀取相簿二維碼

iOS 讀取相簿二維碼

   iOS讀取相簿二維碼請使用官方的方法.現在開始練習起來.

func beginQRImage(image:UIImage) -> Void {
        
        let detector = CIDetector(ofType: CIDetectorTypeQRCode, context: nil, options: [CIDetectorAccuracy: CIDetectorAccuracyHigh]);

        let url = URL(fileURLWithPath: Bundle.main.path(forResource: "timg", ofType: "jpeg")!);
        let ciImage = CIImage(contentsOf: url);
        
        let feature = detector?.features(in: ciImage!);
        
        for item in feature! {
            print("type = \(item.type)");
            if let qrFeature = item as? CIQRCodeFeature {
                print(qrFeature.messageString);
            }
            
        }
        
    }

  就這個以方法就可了.

 哦哦,忘了說一下,需要匯入CoreImage這個庫才行哦.微笑微笑