我使用CGBitmapContextCreate创建了一个上下文.我需要使用CGContextRelease释放它吗?我知道在Objective-C中答案是肯定的,但在Swift中怎么样?
谢谢!
所以,我正在截取一个子类UIView的截图,我将其保存到设备的照片流中.
问题:
问题是我用来resizableImageWithCapInsets为我的UIView添加一个拉伸的背景,但是这个背景在右侧被截断,我不明白为什么.如果有人可以帮助我,我将非常感激.

我通过以下方式将拉伸的背景添加到我的UIView:
[diagramBase addSubview:[self addTileBackgroundOfSize:diagramBase.frame
andType:@"ipad_diagram_border.png"]];
Run Code Online (Sandbox Code Playgroud)
哪个调用此方法:
- (UIImageView *) addTileBackgroundOfSize:(CGRect)frame
andType:(NSString *)type
{
frame.origin.x = 0.0f;
frame.origin.y = 0.0f;
UIImageView *backgroundView = [[UIImageView alloc] initWithFrame:frame];
UIImage *image = [UIImage imageNamed:type];
UIEdgeInsets insets = UIEdgeInsetsMake(10.0f, 10.0f, 10.0f, 10.0f);
UIImage *backgroundImage = [image resizableImageWithCapInsets:insets];
backgroundView.image = backgroundImage;
return backgroundView;
}
Run Code Online (Sandbox Code Playgroud)
实际的printcreen是用这个方法完成的(RINDiagramView是我的子类UIView的名字,我正在截取它的截图).旋转是在那里,因为我需要在保存时旋转图像,但我注释掉那部分,这不是背景的行为怪异.
- (UIImage *) createSnapshotOfView:(RINDiagram *) view
{
CGRect rect = [view bounds];
rect.size.height = rect.size.height - 81.0f;
UIGraphicsBeginImageContextWithOptions(rect.size, YES, 0.0f);
CGContextRef context = UIGraphicsGetCurrentContext();
[view.layer renderInContext:context]; …Run Code Online (Sandbox Code Playgroud) 我怎样才能将CGContextAddArc中的所有像素点到NSMutableArray.or CGContextRef到NSMutable Array
static inline float radians(double degrees)
{
return degrees * PI / 180;
}
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGRect parentViewBounds = self.bounds;
CGFloat x = CGRectGetWidth(parentViewBounds)/2;
CGFloat y = CGRectGetHeight(parentViewBounds)/2;
UIColor *rbg=[UIColor redColor];
CGContextSetFillColor(context, CGColorGetComponents( [rbg CGColor]));
CGContextMoveToPoint(context, x, y);
CGContextAddArc(context, x, y,100,radians(35),radians(105),0);
CGContextClosePath(context);
CGContextFillPath(context);
// here I want to store All points Containing in the Arc to NSMutableArray
NSMutableArray *pointsArray=????
}
Run Code Online (Sandbox Code Playgroud)
图片
我正在使用CGContextRef绘制线图.我可以放大缩小此图表以清楚地显示线条.
我正在使用此代码.
CGContextRef context=UIGraphicsGetCurrentContext();
CGContextBeginPath(context);
CGMutablePathRef path=CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, lastPointX, lastPointY);
CGPathAddLineToPoint(path, NULL, newPointX, newPointY);
CGContextAddPath(context, path);
CGContextSetLineWidth(context, lineWidth);
CGContextSetStrokeColorWithColor(context, lineColor);
CGContextStrokePath(context);
CGPathRelease(path);
if (isFilling) {
CGMutablePathRef path=CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, newPointX, newPointY);
CGPathAddLineToPoint(path, NULL, newPointX, self.bounds.size.height);
CGPathAddLineToPoint(path, NULL, lastPointX, self.bounds.size.height);
CGPathAddLineToPoint(path, NULL, lastPointX, lastPointY);
CGPathCloseSubpath(path);
CGContextAddPath(context, path);
CGContextSetFillColorWithColor(context, fillingColor);
CGContextFillPath(context);
CGPathRelease(path);
}
Run Code Online (Sandbox Code Playgroud)
注意: - 我不想缩放视图.我想重新绘制线条以清楚地显示.
core-graphics objective-c ios cgcontextref cgcontextdrawpath
我有问题在我的绘图中实现"浮雕/阴影效果".手指绘画功能目前与我的自定义工作正常,UIView下面是我的drawRect方法代码:
使用所有方法编辑代码:
- (void)drawRect:(CGRect)rect
{
CGPoint mid1 = midPoint(previousPoint1, previousPoint2);
CGPoint mid2 = midPoint(currentPoint, previousPoint1);
CGContextRef context = UIGraphicsGetCurrentContext();
[self.layer renderInContext:context];
CGContextMoveToPoint(context, mid1.x, mid1.y);
CGContextAddQuadCurveToPoint(context, previousPoint1.x, previousPoint1.y, mid2.x, mid2.y);
CGContextSetLineCap(context, kCGLineCapRound);
CGContextSetLineWidth(context, self.lineWidth);
CGContextSetStrokeColorWithColor(context, self.lineColor.CGColor);
CGContextSaveGState(context);
// for shadow effects
CGContextSetShadowWithColor(context, CGSizeMake(0, 2),3, self.lineColor.CGColor);
CGContextStrokePath(context);
[super drawRect:rect];
}
CGPoint midPoint(CGPoint p1, CGPoint p2)
{
return CGPointMake((p1.x + p2.x) * 0.5, (p1.y + p2.y) * 0.5);
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject]; …Run Code Online (Sandbox Code Playgroud) 我想在文档目录中创建pdf,并希望提供页码,所以我需要CGPDFDocumentRef对象.
let fileName: NSString = "test.pdf"
let path:NSArray = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
let documentDirectory: AnyObject = path.objectAtIndex(0)
let pdfPathWithFileName = documentDirectory.stringByAppendingPathComponent(fileName as String)
UIGraphicsBeginPDFContextToFile(pdfPathWithFileName as String, CGRectZero, nil)
let ref : CGContextRef = UIGraphicsGetCurrentContext()!
let localUrl = NSURL.fileURLWithPath(pdfPathWithFileName)
Run Code Online (Sandbox Code Playgroud)
我已经在url中转换了文件路径,但这下面的行会导致崩溃,我不知道为什么......?
let pdfDocumentRef: CGPDFDocumentRef = CGPDFDocumentCreateWithURL(localUrl as CFURLRef)!
Run Code Online (Sandbox Code Playgroud) 我可以drawRect使用外部方法绘制圆形,矩形,线条等形状
CGContextRef contextRef = UIGraphicsGetCurrentContext();
Run Code Online (Sandbox Code Playgroud)
或者drawRect仅在内部使用它是强制性的.请帮帮我,让我知道如何在drawRect方法外绘制形状.实际上我想继续在touchesMoved事件上绘制点.
这是我绘制点的代码.
CGContextRef contextRef = UIGraphicsGetCurrentContext();
CGContextSetRGBFillColor(contextRef, 0, 255, 0, 1);
CGContextFillEllipseInRect(contextRef, CGRectMake(theMovedPoint.x, theMovedPoint.y, 8, 8));
Run Code Online (Sandbox Code Playgroud) 我有一个自定义的UIView,它绘制了一条这样的路径
- (void)drawRect:(CGRect)rect{
[[UIColor blackColor] setStroke];
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetFillColor(ctx, CGColorGetComponents(self.color));
CGContextFillPath(ctx);
UIBezierPath *thePath = [UIBezierPath bezierPath];
thePath.lineWidth = _lineWidth;
_center = CGPointMake(rect.size.width, rect.size.height);
[thePath moveToPoint:_center];
[thePath addArcWithCenter:_center radius:rect.size.width startAngle:M_PI endAngle:M_PI+degreesToRadians(_angle)clockwise:YES];
[thePath closePath];
[thePath fill];
}
Run Code Online (Sandbox Code Playgroud)
我的自定义UIView也有一种改变填充颜色的方法
- (void) changeColor:(UIColor *) newColor {
self.color = [newColor CGColor];
[self setNeedsDisplay];
}
Run Code Online (Sandbox Code Playgroud)
如果我使用任何预定义颜色调用changeColor:方法,例如
[UIColor redColor]
Run Code Online (Sandbox Code Playgroud)
一切正常.相反,如果我试图给它一个自定义的颜色,如
UIColor* color = [UIColor colorWithRed:1.0f green:0.0f blue:0.0f alpha:1.0f];
Run Code Online (Sandbox Code Playgroud)
我自定义UIView的颜色随机闪烁在白色,红色和蓝色之间.
任何想法为什么?
先感谢您!
在我的应用程序中有一个imageViewA(框架{0,0,320,200}),我想用imageViewA的中心剪切一个圆,半径= 50,并绘制到另一个imageViewB(框架{0,0,100,100}); 像这样的原始图像效果:

并使用下面的代码来剪辑:
UIGraphicsBeginImageContext(self.frame.size);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGFloat height = self.bounds.size.height;
CGContextTranslateCTM(ctx, 0.0, height);
CGContextScaleCTM(ctx, 1.0, -1.0);
CGContextAddArc(ctx, self.frame.size.width/2, self.frame.size.height/2, 50, 0, 2*M_PI, 0);
CGContextClosePath(ctx);
CGContextSaveGState(ctx);
CGContextClip(ctx);
CGContextDrawImage(ctx, CGRectMake(0,0,self.frame.size.width, self.frame.size.height), image.CGImage);
CGContextRestoreGState(ctx);
CGImageRef imageRef = CGBitmapContextCreateImage (ctx);
UIImage *newImage = [UIImage imageWithCGImage:imageRef];
NSString *headerPath = [NSString stringWithFormat:@"%@/header.png",HomeDirectory];
NSData *imageData = UIImageJPEGRepresentation(newImage, 1.0);
if([imageData writeToFile:headerPath atomically:YES]){
imageViewB.image = [UIImage imageWithContentsOfFile:headerPath];
}
Run Code Online (Sandbox Code Playgroud)
剪辑图像效果如下:

我只需要圆形视图,但剪辑效果显示imageViewB有空白区域.如何正确剪辑此图片?谢谢!
我在UIImage类别中编写了一些代码,该类别接受一个UIImage(自身),然后使用CGContextRef重绘它。使用CGBitmapContext时有什么方法可以重写UIImage(自身)吗?当前,我可以返回通过CGContextRef创建的图像,但是可以在该方法中重写图像本身吗?