简体   繁体   中英

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:

#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.

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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