title: ZXingObjc - Converting object c to swift
body: <p>I'm a newbie with Ios. i'm learning swift and overlooked object c.</p>
  
  <p>Currently, i'm writing an demo with swift and xcode 6.1 which can scan qrcode and barcode from camera or an image from image library.</p>
  
  <p>before, i tried using zbar SDK to do this, but it happened error and i didn't know how to fix it, i posted this error in post: <a href="https://stackoverflow.com/questions/28317071/scan-qrcode-and-barcode-from-camera-and-image-which-picked-from-image-library-in">Scan qrcode and barcode from camera and image which picked from image library in swift</a> , but nobody answered.</p>
  
  <p>i'm trying using ZXingObjC to scan qrcode and barcode from image and camera <a href="https://github.com/TheLevelUp/ZXingObjC" rel="nofollow noreferrer">https://github.com/TheLevelUp/ZXingObjC</a> , i read its usage and tried to convert to swift. but it happened error and i don't know how to fix it.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>import UIKit
  
  class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
  
      @IBOutlet weak var lblResult: UILabel!
      @IBOutlet weak var imgView: UIImageView!
      var imagePicker = UIImagePickerController()
  
      override func viewDidLoad() {
          super.viewDidLoad()
          imagePicker.delegate = self
      }
  
      override func didReceiveMemoryWarning() {
          super.didReceiveMemoryWarning()
          // Dispose of any resources that can be recreated.
      }
  
      @IBAction func scanCode(sender: AnyObject) {
          imagePicker.sourceType = .PhotoLibrary
          imagePicker.allowsEditing = false
          presentViewController(imagePicker, animated: true, completion: nil)
      }
  
      func imagePickerController(picker: UIImagePickerController!, didFinishPickingMediaWithInfo info: NSDictionary!) {
          var tempImage:UIImage = info[UIImagePickerControllerOriginalImage] as UIImage
          imgView.contentMode = .ScaleAspectFit
          imgView.image  = tempImage
          dismissViewControllerAnimated(true, completion: nil)
  
          //====&gt; object c code &lt;=====
          /*
          ZXLuminanceSource *source = [[[ZXCGImageLuminanceSource alloc] initWithCGImage:imageToDecode] autorelease];
          ZXBinaryBitmap *bitmap = [ZXBinaryBitmap binaryBitmapWithBinarizer:[ZXHybridBinarizer binarizerWithSource:source]];
  
          NSError *error = nil;
          ZXDecodeHints *hints = [ZXDecodeHints hints];
          ZXMultiFormatReader *reader = [ZXMultiFormatReader reader];
          ZXResult *result = [reader decode:bitmap
              hints:hints
              error:&amp;error];
          if (result) {
          }
          */
  
          //====&gt; Convert to swift and happen error &lt;=====
          let source: ZXLuminanceSource = ZXCGImageLuminanceSource(initWithCGImage: tempImage)
          let binazer: ZXHybridBinarizer = ZXHybridBinarizer(source: source)
          let bitmap: ZXBinaryBitmap = ZXBinaryBitmap(binarizer: binazer)
  
          var error: NSError?
  
          var hints: ZXDecodeHints = ZXDecodeHints()
          var reader: ZXMultiFormatReader = ZXMultiFormatReader()
          var result: ZXResult = reader(bitmap, hints:hints, error: error)
          if (result) {
              lblResult.text = result.text;
          }
      }
  
  }
  </code></pre>
  
  <p>I will be very grateful if someone let me know why it happen error and how to fix it (please give detail instructions because i have just learned swift and ios for 3 weeks without learning object c). Thanks.</p>
  
  <p>Edited: 
  This code worked for me.</p>
  
  <pre><code>let source: ZXLuminanceSource = ZXCGImageLuminanceSource(CGImage: tempImage.CGImage)
  let binazer = ZXHybridBinarizer(source: source)
  let bitmap = ZXBinaryBitmap(binarizer: binazer)
  var error: NSError?
  let hints: ZXDecodeHints = ZXDecodeHints.hints() as ZXDecodeHints
  var reader = ZXMultiFormatReader()
  
  if let result = reader.decode(bitmap, hints: hints, error: &amp;error) {
    lblResult.text = result.text;
  }
  </code></pre>