Objective-c 绘制图像



我正在尝试在触摸页面上绘制图像。

我这里有这种方法,注释掉的代码将绘制一个矩形并且有效,但是当我尝试绘制图像时,它根本不起作用:

- (void)draw
{
    //CGContextRef context = UIGraphicsGetCurrentContext();
    // set the properties
    //CGContextSetAlpha(context, self.lineAlpha);
    // draw the rectangle
    //CGRect rectToFill = CGRectMake(self.firstPoint.x, self.firstPoint.y, self.lastPoint.x - self.firstPoint.x, self.lastPoint.y - self.firstPoint.y);
    //CGContextSetFillColorWithColor(context, self.lineColor.CGColor);
    //CGContextFillRect(UIGraphicsGetCurrentContext(), rectToFill);
    UIImage *_originalImage = [UIImage imageNamed: @"approved"]; // your image
    UIGraphicsBeginImageContext(_originalImage.size);
    CGContextRef _context = UIGraphicsGetCurrentContext(); // here you don't need this reference for the context but if you want to use in the future for drawing anything else on the context you could get it for it
    [_originalImage drawInRect:CGRectMake(0.f, 0.f, _originalImage.size.width, _originalImage.size.height)];
    UIImage *_newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
}

这是我的整个代码:

#pragma mark - LazyPDFDrawingApproved
@interface LazyPDFDrawingApproved ()
@property (nonatomic, assign) CGPoint firstPoint;
@property (nonatomic, assign) CGPoint lastPoint;
@end
#pragma mark -
@implementation LazyPDFDrawingApproved
@synthesize lineColor = _lineColor;
@synthesize lineAlpha = _lineAlpha;
@synthesize lineWidth = _lineWidth;
- (void)setInitialPoint:(CGPoint)firstPoint
{
    self.firstPoint = firstPoint;
}
- (void)moveFromPoint:(CGPoint)startPoint toPoint:(CGPoint)endPoint
{
    self.lastPoint = endPoint;
}
- (void)draw
{
    //CGContextRef context = UIGraphicsGetCurrentContext();
    // set the properties
    //CGContextSetAlpha(context, self.lineAlpha);
    // draw the rectangle
    //CGRect rectToFill = CGRectMake(self.firstPoint.x, self.firstPoint.y, self.lastPoint.x - self.firstPoint.x, self.lastPoint.y - self.firstPoint.y);
    //CGContextSetFillColorWithColor(context, self.lineColor.CGColor);
    //CGContextFillRect(UIGraphicsGetCurrentContext(), rectToFill);
    UIImage *_originalImage = [UIImage imageNamed: @"approved"]; // your image
    UIGraphicsBeginImageContext(_originalImage.size);
    CGContextRef _context = UIGraphicsGetCurrentContext(); // here you don't need this reference for the context but if you want to use in the future for drawing anything else on the context you could get it for it
    [_originalImage drawInRect:CGRectMake(0.f, 0.f, _originalImage.size.width, _originalImage.size.height)];
    UIImage *_newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
}
- (void)dealloc
{
    self.lineColor = nil;
#if !LazyPDF_HAS_ARC
    [super dealloc];
#endif
}
@end

我做错了什么?

更新

更多代码,这些方法调用 draw:

#pragma mark - Drawing
- (void)drawRect:(CGRect)rect
{
#if PARTIAL_REDRAW
    // TODO: draw only the updated part of the image
    [self drawPath];
#else
    [self.image drawInRect:self.bounds];
    [self.currentTool draw];
#endif
}
- (void)updateCacheImage:(BOOL)redraw
{
    // init a context
    UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0.0);
    if (redraw) {
        // erase the previous image
        self.image = nil;
        // load previous image (if returning to screen)
        [[self.prev_image copy] drawInRect:self.bounds];
        // I need to redraw all the lines
        for (id<LazyPDFDrawingTool> tool in self.pathArray) {
            [tool draw];
        }
    } else {
        // set the draw point
        [self.image drawAtPoint:CGPointZero];
        [self.currentTool draw];
    }
    // store the image
    self.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
}

draw 方法中,无需开始新的图像上下文。

在调用该方法之前,您已经启动了图像上下文,因此可以直接绘制到该上下文中。

例如:

-(void) draw {
    CGContextRef context = UIGraphicsGetCurrentContext(); // not needed, but if you're doing other drawing, it'll be needed.
    UIImage *_originalImage = [UIImage imageNamed: @"approved"]; // your image
    [_originalImage drawInRect:CGRectMake(0.f, 0.f, _originalImage.size.width, _originalImage.size.height)]; 
}
新图像上下文在此处

有意义的唯一原因是,如果要对图像本身应用转换,因此需要与图像大小相同的上下文。

当前代码不起作用的原因是,您正在创建一个新的图像上下文,绘制到其中,从结果创建UIImage,但随后您没有对该图像执行任何操作。因此,它永远不会到达屏幕。

如果你想这样做,那么你需要在输出的图像上再次调用drawInRect:(在你的例子中_newImage),以便将其绘制到前面的上下文中。

最新更新