在我的应用程序中,我试图沿着路径渲染文本; 这对大多数人物来说都没问题,但对于日语(或任何非mac-Roman)都没有.我被建议使用[NSString drawAtPoint],它在我的CATiledLayer中显示正确的字符; 然而,它们在大约5秒后消失.在这个时候我可以缩放图层并且它们可以正确缩放,但它们似乎没有像文本的其他部分那样被提交到CATiledLayer.
在渲染之前,我检查字符串并确定它们中的任何一个是否都不可渲染.如果我有问题,我会改用drawAtpoint:
if (!isFullyDisplayable)
{
CGContextShowGlyphsAtPoint(context, pt.x, pt.y, realGlyph, 1);
}
else {
// fall back on less flexible font rendering for difficult characters
NSString *b = [gv text];
NSString *c = [b substringWithRange:NSMakeRange(j,1)];
[c drawAtPoint:pt withFont:[UIFont fontWithName:@"Helvetica-Bold" size:16.0]];
}
Run Code Online (Sandbox Code Playgroud)
有没有人有关于为什么文本消失的指示?
一旦使用drawAtPoint,我的调试输出就会充满:
<Error>: CGContextGetShouldSmoothFonts: invalid context
<Error>: CGContextSetFont: invalid context
<Error>: CGContextSetTextMatrix: invalid context
<Error>: CGContextSetFontSize: invalid context
<Error>: CGContextSetTextPosition: invalid context
<Error>: CGContextShowGlyphsWithAdvances: invalid context
Run Code Online (Sandbox Code Playgroud)
所以我猜测它与我的上下文管理有关,但我认为如果这与我使用CGContextShowGlyphsAtPoint在同一个地方它应该有正确的上下文?
我得到了一些与准备UIImage一个相关的问题UIView.我有时(但不总是)看到错误
<Error>: CGContextSaveGState: invalid context 0x0
Run Code Online (Sandbox Code Playgroud)
我正在使用iPhone SDK 4.0.我的代码:
-(void)drawRect:(CGRect)rect
{
// Customized to draw some text
}
-(UIImage*) PrepareBackImage:(CGRect) aRect // aRect = (0,0,1800,1200)
{
UIImage* background;
background = [self GetImageFromView:self toRect:self.frame];
UIGraphicsBeginImageContext(aRect.size);
CGPoint backgroundPoint = CGPointMake(0,0);
[background drawAtPoint:backgroundPoint];
UIImage* backImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return backImage;
}
- (UIImage *) GetImageFromView:(UIView *)aView toRect:(CGRect)aRect
{
CGSize pageSize = aRect.size;
UIGraphicsBeginImageContext(pageSize);
[aView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
Run Code Online (Sandbox Code Playgroud) 我正在为我为MKMapOverlayView创建的CGContext执行一些CG绘图操作.在绘制到我的上下文后,我创建了一个图像并将其粘贴到MapKit提供的上下文中.
- (void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)context {
CGColorSpaceRef colorRef = CGColorSpaceCreateDeviceRGB();
CGContextRef myContext = CGBitmapContextCreate(NULL, kTileSize, kTileSize, 8, 0, colorRef, kCGImageAlphaPremultipliedLast);
CGColorSpaceRelease(colorRef);
CGContextSetAllowsAntialiasing(myContext, TRUE);
//...cut out drawing operations...
CGImageRef image = CGBitmapContextCreateImage(myContext);
CGContextDrawImage(context, [self rectForMapRect:mapRect], image);
CGImageRelease(image);
CGContextRelease(myContext);
}
Run Code Online (Sandbox Code Playgroud)
有没有办法简单地复制myContext到context无需创建图像?
我意识到你们中的一些人会说"为什么不直接画入MapKit提供的上下文".不幸的是,我们在context直接渲染时遇到了绘图故障.Apple目前正在为我们调查这个问题,但与此同时我们需要找到一个解决方法.我上面介绍的这种解决方法是我的"最佳"镜头,但它有点慢.
PS我已经开始了赏金,因为我也在寻找答案.具体来说,我的目标是OS X.所以答案应该在那里工作.OP正在寻找iOS上的答案.
我正在尝试保存和恢复CGContext以避免第二次进行繁重的绘图计算而我收到了错误<Error>: CGGStackRestore: gstack underflow.
我究竟做错了什么?这样做的正确方法是什么?
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
if (initialized) {
CGContextRestoreGState(context);
//scale context
return;
}
initialized = YES;
//heavy drawing computation and drawing
CGContextSaveGState(context);
}
Run Code Online (Sandbox Code Playgroud) 有人知道如何获取从UIWebView呈现的UIImage的方式或创意吗?问题是,必须在后台线程上.
我会详细说明:我试图每秒从多个UIWebView获取图像+ - 并将其显示在屏幕上(当然是iPhone).因为将图层渲染到CGContext是一个CPU消耗作业,我不想使用主线程,所以不要挂起UI.
我到目前为止的尝试是:
我试图renderInContext在webView的图层上使用UIGraphicalContext,但_WebTryThreadLock错误导致网页浏览崩溃.
我尝试创建一个CGBitmapContext,并将webview的图层渲染到它,但得到了相同的结果.
我尝试实现一个copy方法(通过添加一个类别)CALayer,深度复制所有的公共属性和子图层.之后,我尝试renderInContext了我复制的图层.我得到了一个部分 "正确" 的UIImage - 意思是,并非所有的图层都被渲染,所以例如,我只会得到网站标题(或页脚,或正文,或搜索栏,或只是一些框架).UIWebview的图层由所有类型的子类CALayers组成,因此这可能就是为什么这个接近不起作用的原因.
我尝试kCATransactionDisableActions在a中设置CATransaction,但它似乎没有改变这种行为(它们都没有).
我非常接近放弃.
你们中间有救世主吗?
我想将UIView渲染成CGContextRef
-(void)methodName:(CGContextRef)ctx {
UIView *someView = [[UIView alloc] init];
MagicalFunction(ctx, someView);
}
Run Code Online (Sandbox Code Playgroud)
因此,这里的MagicalFunction应该将UIView(可能是它的图层)渲染到当前上下文中.
我怎么做?
提前致谢!
我需要画线; 有些是破灭的,有些是连续的.我不知道哪些是虚线,哪些不是.当我绘制虚线时,我使用这个:
CGContextSetLineDash(context, 5, linedashPattern, 2); // set dashed line
Run Code Online (Sandbox Code Playgroud)
然而,如果我画了一条虚线,那么下一条也都是虚线.是否有重置"虚线"状态,绘制连续线的操作?例如:
CGContextSetLineContinuous(context,......); // set continuous line
Run Code Online (Sandbox Code Playgroud) 我正在为iPhone制作一款可以播放一些电影的应用程序MPMoviePlayerViewController.到目前为止,我已经得到它来播放电影,但它在调试器中抛出一些错误,一切都开始CGContext.我试图解决它已经破坏了我的大脑.以下是我的代码的详细信息:
.h文件:
#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>
@interface MovieViewController : UIViewController {
MPMoviePlayerViewController *playerController;
}
-(IBAction) playMovie:(id)sender;
Run Code Online (Sandbox Code Playgroud)
.m文件:
@interface MovieViewController ()
@end
@implementation
-(IBAction)playMovie:(id)sender {
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:@"moviename" ofType:@"mp4"]];
playerController = [[MPMoviePlayerViewController alloc]
initWithContentURL:url];
[self presentMoviePlayerViewControllerAnimated:playerController];
playerController.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
[playerController.moviePlayer play];
playerController = nil;
}
Run Code Online (Sandbox Code Playgroud)
当我运行该程序时,电影将播放,但是当代码执行该行时:playerController = [[MPMoviePlayerViewController alloc] initWithContentURL:url];发生以下错误:
<Error>: CGContextSaveGState: invalid context 0x0
<Error>: CGContextClipToRect: invalid context 0x0
<Error>: CGContextTranslateCTM: invalid context 0x0
<Error>: CGContextDrawShading: invalid context 0x0
<Error>: …Run Code Online (Sandbox Code Playgroud) 我可以使用图像实现交替条纹图案填充UIColor(patternImage: UIImage(named: "pattern.png")).
但是,如何通过一些简单的紧凑代码以编程方式实现交替条纹图案填充?
以下是我想要实现的两个例子.
题
使用代码,如何
UIView使用Swift 填充不同的交替颜色条纹?(1)从上到下(90度)运行的双色交替模式?
(2)从左上到右下(45度)运行的三色交替模式?
cgcontext ×10
iphone ×7
ios ×6
objective-c ×3
swift ×2
uiview ×2
calayer ×1
catiledlayer ×1
cocoa-touch ×1
ipad ×1
line ×1
optimization ×1
uiwebview ×1
xcode ×1