0

我目前正在编辑 FTCoreText 模块以直接从网络加载图像并使用 Core Text 显示它们。
因此,我对https://github.com/FuerteInternational/FTCoreText/blob/master/FTCoreText/classes/FTCoreTextView.m#L1149进行了一些编辑,以使用 AFNetworking 的 UIImageView 类别。现在,我使用一个块从互联网下载图像并将其绘制到视图中。图像已绘制,但上下文有误:

Aug 13 05:34:50 Yohann-iPhone CCFA[9878] <Error>: CGContextSaveGState: invalid context 0x0
Aug 13 05:34:50 Yohann-iPhone CCFA[9878] <Error>: CGContextSaveGState: invalid context 0x0
Aug 13 05:34:50 Yohann-iPhone CCFA[9878] <Error>: CGContextSetBlendMode: invalid context 0x0
Aug 13 05:34:50 Yohann-iPhone CCFA[9878] <Error>: CGContextSetAlpha: invalid context 0x0
Aug 13 05:34:50 Yohann-iPhone CCFA[9878] <Error>: CGContextTranslateCTM: invalid context 0x0
Aug 13 05:34:50 Yohann-iPhone CCFA[9878] <Error>: CGContextScaleCTM: invalid context 0x0
Aug 13 05:34:50 Yohann-iPhone CCFA[9878] <Error>: CGContextDrawImage: invalid context 0x0
Aug 13 05:34:50 Yohann-iPhone CCFA[9878] <Error>: CGContextRestoreGState: invalid context 0x0

你可以在这里看到我的块:

UIImageView *imgNode = [[UIImageView alloc] init];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:imageNode.imageName]];
[imgNode setImageWithURLRequest:request placeholderImage:[UIImage imageNamed:@"rien.gif"] success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
                if (alignment == kCTRightTextAlignment) x = (self.frame.size.width - image.size.width);
                if (alignment == kCTCenterTextAlignment) x = ((self.frame.size.width - image.size.width) / 2);

                frame = CGRectMake(x, (lineFrame.origin.y - image.size.height), image.size.width, image.size.height);


                if (alignment != kCTCenterTextAlignment) frame.origin.x = (alignment == kCTLeftTextAlignment)? insets.left : (self.frame.size.width - image.size.width - insets.right);
                frame.origin.y += insets.top;
                frame.size.width = ((insets.left + insets.right + image.size.width ) > self.frame.size.width)? self.frame.size.width : image.size.width;


                frame = CGRectMake((((320-2*20)-image.size.width)/2), frame.origin.y, image.size.width, image.size.height);
                NSLog(@"Frame : %f %f", frame.size.width, frame.size.height);
                NSLog(@"Image : %f %f", image.size.width, image.size.height);
                CGContextRef context = UIGraphicsGetCurrentContext();                       
                CGContextSaveGState(context);

                [image drawInRect:CGRectIntegral(frame)];
            } failure:nil];
            [imgNode.image drawInRect:CGRectIntegral(frame)];

当然,我将整个代码粘贴到你这里:http: //pastebin.com/Qj6PtEFj

我希望你能帮帮我 :) !

4

2 回答 2

1

我有类似的问题,并用UIGraphicsPushContext(context)/UIGraphicsPushContext(context)绘图代码解决了它。上下文是用

CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(NULL, width, height, 8, 0, space, kCGImageAlphaPremultipliedLast);
于 2013-03-26T22:45:03.823 回答
0

只是建议,如果您遇到错误,例如

“CGContextSaveGState:无效的上下文 0x0”</p>

意味着您没有在正确的上下文中工作,这UIGraphicsGetCurrentContext()是不正确的。获取绘制发生的原始上下文并将其传递给完整的块。

于 2012-08-24T01:59:04.873 回答