objective-c绘制图像

时间:2016-03-10 14:50:40

标签: ios 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();
}

1 个答案:

答案 0 :(得分:4)

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),以便将其绘制到上一个上下文中。