简体   繁体   English

Objective-C绘制图像

[英]objective-c draw an image

I am trying to draw an image on a page on touch. 我试图在触摸页面上绘制图像。

I have this method here, the comment out code will draw a rectangle and that works, but when I try to draw an image, it does not work at all: 我在这里有此方法,注释掉的代码将绘制一个矩形并且可以工作,但是当我尝试绘制图像时,它根本不起作用:

- (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();

}

Here is my entire code: 这是我的整个代码:

#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

What am I doing wrong? 我究竟做错了什么?

UPDATE 更新

More code, these methods call draw: 更多代码,这些方法调用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();
}

In your draw method, you don't need to begin a new image context. 在您的draw方法中,您无需开始新的图像上下文。

You already begin an image context before invoking the method, so you can draw directly into that. 在调用该方法之前,您已经开始了图像上下文,因此可以直接使用它。

For example: 例如:

-(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)]; 

}

The only reason a new image context would make sense here is if you were applying transforms to the image itself, and therefore required a context that was the same size as the image. 新图像上下文在这里有意义的唯一原因是,如果您对图像本身应用了转换,因此需要一个与图像大小相同的上下文。

The reason your current code doesn't work is that you're creating a new image context, drawing into it, creating a UIImage from the result, but then you're not doing anything with that image. 当前代码不起作用的原因是,您正在创建一个新的图像上下文,将其绘制到该上下文中,并根据结果创建一个UIImage ,但随后您对该图像不做任何事情。 Therefore it'll never reach the screen. 因此,它将永远不会到达屏幕。

If you wanted to do it that way, then you would need to call drawInRect: again on your outputted image ( _newImage in your case) in order to draw it into the previous context. 如果您想这样做,则需要在输出的图像上再次调用drawInRect:在您的情况下为_newImage ),以将其绘制到先前的上下文中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM