1

I took an entire class from another Objective-C project and tried to implement it in my Swift project. However, I've received the following error:

Error: CGBitmapContextGetWidth: invalid context 0x608000361440. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.

Error: CGBitmapContextGetHeight: invalid context 0x608000361440. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.

Error: CGContextDrawImage: invalid context 0x608000361440. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.


My code:

- (UIImage *)drawStyleOneWithTemplate:(TextFormatterTemplate *)template
{
    self.currentTemplate = template;

    NSArray *arrayOfFonts = template.fontsNameArray;
    int xOffset = 0;
    int yOffset = template.rowSpace;

    CGFloat prevHeight = yOffset;

    // Initialize a graphics context
    UIGraphicsBeginImageContext(self.contextSize);
    CGContextRef context = UIGraphicsGetCurrentContext();
    // Flip the context coordinates
    CGContextTranslateCTM(context, 0, self.contextSize.height);
    CGContextScaleCTM(context, 1.0, -1.0);
    // Set the text matrix.
    CGContextSetTextMatrix(context, CGAffineTransformIdentity);

    /**  START DRAWING  **/

    // Draw top image
    if (template.hasTopImage && template.topImagesArray.count > 0)
        {
        NSInteger randImgId = [self randIntRange:template.topImagesArray.count];
        UIImage *topImg = [self changeColor:[UIColor whiteColor] inImage:[UIImage imageNamed:template.topImagesArray[randImgId]]];

        CGSize imageSize = [self scaleDividerImageSize:topImg.size withWidth:self.contextSize.width*template.topImageScale];
        CGFloat imageOffset = (CGBitmapContextGetWidth(context) - imageSize.width) / 2;
        //Exactly after this line appear first error
        CGContextDrawImage(context, CGRectMake(imageOffset, CGBitmapContextGetHeight(context) - imageSize.height, imageSize.width, imageSize.height), topImg.CGImage);
        prevHeight += imageSize.height;
        topImg = nil;
        }
    //From there a lot of similar errors
}

It seems like those are only warnings but it all ends here :

- (void)resizeContext:(CGContextRef *)context toSize:(CGSize)newSize
{
    CGImageRef outputImage = CGBitmapContextCreateImage(*context);

    // Initialize a graphics context
    UIGraphicsBeginImageContext(newSize);
    CGContextRef newContext = UIGraphicsGetCurrentContext();
    // Flip the context coordinates
    CGContextTranslateCTM(newContext, 0, newSize.height);
    CGContextScaleCTM(newContext, 1.0, -1.0);
    // Set the text matrix.
    CGContextSetTextMatrix(newContext, CGAffineTransformIdentity);

    CGContextDrawImage(newContext,
                       CGRectMake(0,
                                  newSize.height - CGImageGetHeight(outputImage),
                                  CGImageGetWidth(outputImage),
                                  CGImageGetHeight(outputImage)),
                       outputImage);

    CGImageRelease(outputImage);
    CGContextRelease(*context); // <-- The app died here
    *context = newContext;

    //    UIGraphicsEndImageContext(); // This line was commented already
}

I repeat, I have just implemented this file in my Swift project, so i didn't modify anything. Please help!

EDIT: After adding CG_CONTEXT_SHOW_BACKTRACE, Backtrace for first error (they are quite similar):

CGBitmapContextGetWidth: invalid context 0x608000176d40. Backtrace: CGBitmapContextGetWidth+51 [TextFormatter drawStyleOneWithTemplate:]+1266 [TextFormatter createImageWithTextTemplate:]+1559 __54-[TextFormatter textFormatterDrawTextWithStyle:block:]_block_invoke+45 _dispatch_call_block_and_release+12 _dispatch_client_callout+8 _dispatch_queue_override_invoke+1426 _dispatch_root_queue_drain+720 _dispatch_worker_thread3+123 _pthread_wqthread+1129

Community
  • 1
  • 1
  • what happens when you [set the `CG_CONTEXT_SHOW_BACKTRACE` environment variable](https://stackoverflow.com/questions/17393053/where-to-set-environment-variables-for-app)? – Michael Dautermann Jun 18 '17 at 09:25
  • 2
    You shouldn't `CGContextRelease` because you didn't create it. You end the context (which you've commented out), not release it. Every `UIGraphicsBeginImageContext` must be matched with a corresponding `UIGraphicsEndImageContext`. – Rob Jun 18 '17 at 10:01
  • @MichaelDautermann I added, but i understand like nothing , I am a begginer – Vlad Vaculin Jun 18 '17 at 10:08
  • 1
    @VladVaculin https://stackoverflow.com/a/14064537/1801544 Call only `CF...Release()` if you did `CF...Create()` or `CF...Copy()`, to simplify. `context` wasn't created or copied by you. So you don't need to call release on it. – Larme Jun 18 '17 at 10:19

0 Answers0