gpt4 book ai didi

ios - 我该如何修复 CGContextRestoreGState : invalid context 0x0

转载 作者:可可西里 更新时间:2023-11-01 06:15:38 24 4
gpt4 key购买 nike

这是我正在使用的代码:

    CGRect imageRect = CGRectMake(0, 0, oldImage.size.width, oldImage.size.height);

CGRect newRect = imageRect;

UIGraphicsBeginImageContextWithOptions(newRect.size, NO, oldImage.scale);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextScaleCTM(ctx, 1, -1);
CGContextTranslateCTM(ctx, 0, -(newRect.size.height));
CGContextSaveGState(ctx);
CGContextClipToMask(ctx, newRect, oldImage.CGImage);
CGContextSetFillColorWithColor(ctx, [UIColor colorWithWhite:0 alpha:0].CGColor);
CGContextFillRect(ctx, newRect);
CGContextRestoreGState(ctx);
CGContextClipToMask(ctx, imageRect, oldImage.CGImage);

CGContextSetFillColorWithColor(ctx, tintColor.CGColor);

CGContextFillRect(ctx, imageRect);
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

我继续在控制台中收到错误,我不确定要尝试什么或没有正确完成什么。

最佳答案

上述错误的确切原因:- 由于当前上下文大小为零,因此您要将 clip 添加到实际不存在的当前上下文中。所以上面的错误经常发生。

所以在上面的函数中检查 oldImage 是否存在。

所以更新后的代码看起来像:

    if ( oldImage = [UIImage imageNamed:@"oldImage.png"]) {

CGRect imageRect = CGRectMake(0, 0, oldImage.size.width, oldImage.size.height);
CGRect newRect = imageRect;
UIGraphicsBeginImageContextWithOptions(newRect.size, NO, oldImage.scale);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextScaleCTM(ctx, 1, -1);
CGContextTranslateCTM(ctx, 0, -(newRect.size.height));
CGContextSaveGState(ctx);
CGContextClipToMask(ctx, newRect, oldImage.CGImage);
CGContextSetFillColorWithColor(ctx, [UIColor colorWithWhite:0 alpha:0].CGColor);
CGContextFillRect(ctx, newRect);
CGContextRestoreGState(ctx);
CGContextClipToMask(ctx, imageRect, oldImage.CGImage);
CGContextSetFillColorWithColor(ctx, tintColor.CGColor);
CGContextFillRect(ctx, imageRect);
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
else {
//image do not exist
// Unable to change into New Image
}

关于ios - 我该如何修复 CGContextRestoreGState : invalid context 0x0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18901494/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com