gpt4 book ai didi

iphone - CGContext 错误/警告

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:31:37 24 4
gpt4 key购买 nike

我有以下代码来截取屏幕截图。我特别没有使用 UIGraphicsBeginImageContext,因为之前我发现这会占用大量内存:

    CGSize size = activeView.frame.size;

NSUInteger width = size.width;
NSUInteger height = size.height;

NSLog(@"BEFORE");

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

unsigned char *rawData = malloc(height * width * 4);
memset(rawData,0,height * width * 4);

NSUInteger bytesPerPixel = 4;
NSUInteger bytesPerRow = bytesPerPixel * width;
NSUInteger bitsPerComponent = 8;
CGContextRef context = CGBitmapContextCreate(rawData, width, height, bitsPerComponent, bytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);

CGRect bounds;
bounds.origin = CGPointMake(0,0);
bounds.size = size;

CGContextDrawImage(context, CGRectMake(0, 0, size.width, size.height), [self.topimage CGImage]);
CGContextDrawImage(context, CGRectMake(0, 0, size.width, size.height), [bottomimage CGImage]);

CGImageRef imageRef = CGBitmapContextCreateImage(context);
UIImage *latest = [UIImage imageWithCGImage:imageRef scale:0.0 orientation:UIImageOrientationDownMirrored];

[activeView.layer renderInContext:UIGraphicsGetCurrentContext()];

CGImageRef imageRef2 = CGBitmapContextCreateImage(context);
UIImage *latest2 = [UIImage imageWithCGImage:imageRef2 scale:0.0 orientation:UIImageOrientationDownMirrored];

CGContextRelease(context);
CGImageRelease(imageRef);
CGImageRelease(imageRef2);

NSLog(@"AFTER");

在“BEFORE”和“AFTER”NSLogs 之间,出现以下错误/警告:

<Error>: CGContextSaveGState: invalid context 0x0
<Error>: CGContextSetAlpha: invalid context 0x0
<Error>: CGContextSaveGState: invalid context 0x0
<Error>: CGContextSetFillColorWithColor: invalid context 0x0
<Error>: CGContextAddRect: invalid context 0x0
<Error>: CGContextDrawPath: invalid context 0x0
<Error>: CGContextRestoreGState: invalid context 0x0
<Error>: CGContextSaveGState: invalid context 0x0
<Error>: CGContextGetBaseCTM: invalid context 0x0
<Error>: CGContextConcatCTM: invalid context 0x0
<Error>: CGContextSetBaseCTM: invalid context 0x0
<Error>: CGContextSaveGState: invalid context 0x0
<Error>: CGContextClipToRect: invalid context 0x0
<Error>: CGContextSetAlpha: invalid context 0x0
<Error>: CGContextSaveGState: invalid context 0x0
<Error>: CGContextSetFillColorWithColor: invalid context 0x0
<Error>: CGContextAddRect: invalid context 0x0
<Error>: CGContextDrawPath: invalid context 0x0
<Error>: CGContextRestoreGState: invalid context 0x0
<Error>: CGContextSaveGState: invalid context 0x0
<Error>: CGContextConcatCTM: invalid context 0x0
<Error>: CGContextConcatCTM: invalid context 0x0
<Error>: CGContextClipToRect: invalid context 0x0
<Error>: CGContextDrawImage: invalid context 0x0
<Error>: CGContextRestoreGState: invalid context 0x0
<Error>: CGContextRestoreGState: invalid context 0x0
<Error>: CGContextSetBaseCTM: invalid context 0x0
<Error>: CGContextRestoreGState: invalid context 0x0

为什么会这样?

最佳答案

[activeView.layer renderInContext:UIGraphicsGetCurrentContext()];

这一行就是问题所在……您没有“当前”上下文。请改用 context

关于iphone - CGContext 错误/警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13109487/

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