iOS 创建二维码 偶现闪退 崩溃

2018-07-30  本文已影响0人  青蛙舞

最近工程里捕捉到创建二维码时偶尔会闪退,最后发现跟EAGLContext相关,问题已经处理了,清空EAGLContext,重新设置EAGLContext,有遇到相关问题的网友可评论,我回头贴上处理方案的代码

关键地方是[EAGLContext setCurrentContext:nil];

- (UIImage*)resizeCodeImage:(CIImage*)imagewithSize:(CGSize)size {

    EAGLContext *curContext = [EAGLContext currentContext];

    if(curContext) {

        [EAGLContext setCurrentContext:nil];

    }

    CGRect extent = CGRectIntegral(image.extent);

    CGFloatscaleWidth = size.width/CGRectGetWidth(extent);

    CGFloatscaleHeight = size.height/CGRectGetHeight(extent);

    size_twidth =CGRectGetWidth(extent) * scaleWidth;

    size_theight =CGRectGetHeight(extent) * scaleHeight;

    CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceGray();

    CGContextRef contentRef = CGBitmapContextCreate(nil, width, height, 8, 0, colorSpaceRef, (CGBitmapInfo)kCGImageAlphaNone);

    CGColorSpaceRelease(colorSpaceRef);

    CIContext *context = [CIContext contextWithOptions:nil];

    CGImageRefimageRef = [contextcreateCGImage:imagefromRect:extent];

    CGContextSetInterpolationQuality(contentRef, kCGInterpolationNone);

    CGContextScaleCTM(contentRef, scaleWidth, scaleHeight);

    CGContextDrawImage(contentRef, extent, imageRef);

    CGImageRefimageRefResized =CGBitmapContextCreateImage(contentRef);

    CGContextRelease(contentRef);

    CGImageRelease(imageRef);

    UIImage*img = [UIImageimageWithCGImage:imageRefResized];

    if(curContext) {

        [EAGLContext setCurrentContext:curContext];

    }

    CGImageRelease(imageRefResized);

    returnimg;

}

上一篇下一篇

猜你喜欢

热点阅读