title: Objective C: How to convert CGImageRef to UIImageView?
body: <p>I'm using the <a href="https://github.com/TheLevelUp/ZXingObjC" rel="noreferrer">XZINGObjC</a> framework to create an EAN-Barcode-Image. Following the documentation, I'm doing it like</p>
  
  <pre><code> //in viewDidAppear
  
   //XZING: create Matrix
   NSString* eanString = @"1234567890123";  //sth. like that
   ZXBitMatrix* result = [writer encode:eanString
                                format:kBarcodeFormatEan13
                                 width:500
                                height:500
                                 error:&amp;error];
  if (result) {
        //XZING: convert matrix to CGImageRef
        CGImageRef imageRef = [[ZXImage imageWithMatrix:result] cgimage]; 
  
        //CRASHLINE HERE!! (this is NOT in the XZING documentation, but i cannot figure out the issue!)
        UIImage* uiImage = [[UIImage alloc] initWithCGImage:imageRef];  //&lt;--CRASH: EXC_BAD_ACCESS
  
        if(image != nil){
            //assigning image to ui
            self.barCodeImageView.image = uiImage;   
        }
  </code></pre>
  
  <p><strong>It works, if I step through this code using breakpoints!</strong> However, I think at some point an Image is not ready for use?! But I cannot find the reason.</p>
  
  <p>What I tried:</p>
  
  <ul>
  <li>using <code>imageRef</code> and <code>uiImage</code> as local variables (EXC_BAD_ACCESS CRASH)</li>
  <li>tried that operation in a background thread (EXC_BAD_ACCESS CRASH)</li>
  </ul>
  
  <p>Same here, every solution worked if I used breakpoints and stepped line by line through the code. What is my mistake here? Any ideas? Thanks in advance!</p>