Objective C: How to convert CGImageRef to UIImageView?
cgimageref, exc-bad-access, objective-c, uiimage
Solution
After some try and error programming, I could fix the issue by replacing the following lines
CGImageRef imageRef = [[ZXImage imageWithMatrix:result] cgimage];
UIImage* uiImage = [[UIImage alloc] initWithCGImage:imageRef]; //<--CRASH
with
UIImage* uiImage = [[UIImage alloc] initWithCGImage:[[ZXImage imageWithMatrix:result] cgimage]];
Still, I don't know why?! I'm pretty sure something isn't hold in the memory or maybe the `CGImageRef` isn't ready if I try to convert it to `UIImage`.
Problem
I'm using the XZINGObjC framework to create an EAN-Barcode-Image. Following the documentation, I'm doing it like ``` //in viewDidAppear //XZING: create Matrix NSString* eanString = @"1234567890123"; //sth. like that ZXBitMatrix* result = [writer encode:eanString format:kBarcodeFormatEan13 width:500 height:500 error:&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]; //<--CRASH: EXC_BAD_ACCESS if(image != nil){ //assigning image to ui self.barCodeImageView.image = uiImage; } ``` It works, if I step through this code using breakpoints! However, I think at some point an Image is not ready for use?! But I cannot find the reason. What I tried: - using `imageRef` and `uiImage` as local variables (EXC_BAD_ACCESS CRASH) - tried that operation in a background thread (EXC_BAD_ACCESS CRASH) 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!