Clicky

I am building an image on screen with a new ipad app.  it works as follows:  there is a light grey bounding box that allows users to sign their name within the area.  it writes it on screen, and looks as it is supposed to.  The final destination/purpose of this is to print to a label printer (Zebra gx420d).  This part of the project is working very nicely... it prints perfect!  

Here is what I'd like to do:  I have a Label field with text in it on screen with the drawing area.  I would like it to include the label field in the drawing of the image, but cannot figure out how to.  Please help!!!!

asked 07/30/2011 03:39

14_east's gravatar image

14_east ♦♦


10 Answers:
Can you show a screen shot of what you have or want to have please?
link
darbid73's gravatar image

darbid73

yep, sure can my friend!  attached you will see the grey bounding box area to "draw" in, and the text label: "My Drawing...".

Currently, when printing the drawing on the screen, I can only print the grey drawing area and the actual lines drawn.  What I need to do, is somehow have the UILabel to be a part of the "image" that we are printing...  any ideas?  Should I post the code to make the drawing work?  
 
screenshot of the app in action
screenshot of the app in action
 
link
14_east's gravatar image

14_east

I might not be able to help you with this one but let's see. Are you doing something like this?
1:
2:
3:
4:
5:
6:
7:
8:
UIImage* image = nil;

UIGraphicsBeginImageContext(offscreenWebView_.frame.size);
{
    [offscreenWebView_.layer renderInContext: UIGraphicsGetCurrentContext()];
    image = UIGraphicsGetImageFromCurrentImageContext();
}
UIGraphicsEndImageContext();


This is not my code just something I copied.
link
darbid73's gravatar image

darbid73

here is the M file that I'm using to do the drawing...  there's no reference of the UILabel field (that I wish to include as part of the image) in here yet...
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
#import "DrawingArea.h"


@interface LineSegment : NSObject {
	CGPoint start;
	CGPoint end;
}

@property(nonatomic,assign) CGPoint start;
@property(nonatomic,assign) CGPoint end;

-(id) init:(CGPoint)aStart withEnd:(CGPoint)anEnd;

@end


@implementation LineSegment

@synthesize start;
@synthesize end;

-(id) init:(CGPoint)aStart withEnd:(CGPoint)anEnd{
    self = [super init];
	self.start = aStart;
	self.end = anEnd;
	return self;
}

@end


@implementation DrawingArea

@synthesize listOfLines;
@synthesize previousPoint;
@synthesize signatureImage;
@synthesize drawingAreaGraphicsContext;
@synthesize clearRequest;

- (void)awakeFromNib {
	self.listOfLines = [NSMutableArray arrayWithCapacity:11];
	self.drawingAreaGraphicsContext =  nil;
	self.clearRequest = YES;
}

- (void) drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();
	
	if (self.clearRequest) {
		CGFloat gray[4] = {0.95f, 0.95f, 0.95f, 1.0f};
		CGContextSetFillColor(context, gray);
		CGContextFillRect(context, self.bounds);
		self.clearRequest = NO;
	}
	
    CGFloat black[4] = {0.0f, 0.0f, 0.0f, 1.0f};
    CGContextSetStrokeColor(context, black);
	for(LineSegment* line in self.listOfLines){
		CGContextBeginPath(context);
		CGPoint startPoint = line.start;
		CGPoint endPoint = line.end;
		CGContextMoveToPoint(context, startPoint.x, startPoint.y);
		CGContextAddLineToPoint(context, endPoint.x, endPoint.y);
		CGContextStrokePath(context);
	}
	if (self.signatureImage != nil) {
		CGImageRelease(self.signatureImage);
		self.signatureImage = nil;
	}
	self.signatureImage = CGBitmapContextCreateImage(context);
}

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
	self.previousPoint = [[touches anyObject] locationInView:self];
}

- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
	CGPoint currPoint = [[touches anyObject] locationInView:self];
	LineSegment* line = [[LineSegment alloc] init:self.previousPoint withEnd:currPoint];
	[self.listOfLines addObject:line];
	self.previousPoint = currPoint;
	[line release];
	[self setNeedsDisplay];
}

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
	CGPoint currPoint = [[touches anyObject] locationInView:self];
	LineSegment* line = [[LineSegment alloc] init:self.previousPoint withEnd:currPoint];
	[self.listOfLines addObject:line];
	[line release];
	[self setNeedsDisplay];
}

-(CGImageRef) getImage{
	return self.signatureImage;
}

-(void) clearImage{
	self.clearRequest = YES;
	[self.listOfLines removeAllObjects];
	[self setNeedsDisplay];
}

- (void)dealloc {
	[listOfLines release];
	if (self.signatureImage != nil) {
		CGImageRelease(self.signatureImage);
		self.signatureImage = nil;
	}	
    [super dealloc];
}

@end
link
14_east's gravatar image

14_east

14_east I am guessing here but I have a feeling that it has to do with the fact that UILabel is another UIview and your drawrect function (Quartz) is not picking it up.  I have done a little bit of googling myself but did not get anywhere. Sorry.
link
darbid73's gravatar image

darbid73

that's what i was afraid if... I'll keep at it.
link
14_east's gravatar image

14_east

I was searching around... and someone pointed me to this:  QUESTION:  I have an NSString that I would like to draw it's contents to a UIImage but have absolutely NO idea how I would go about doing this.  (attached was his answer)  

My question to you, is can this example be applied to my project?  What say you?
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
-(UIImage *)imageFromText:(NSString *)text
{
    // set the font type and size
    UIFont *font = [UIFont systemFontOfSize:20.0];  
    CGSize size  = [text sizeWithFont:font];

    // check if UIGraphicsBeginImageContextWithOptions is available (iOS is 4.0+)
    if (UIGraphicsBeginImageContextWithOptions != NULL)
        UIGraphicsBeginImageContextWithOptions(size,NO,0.0);
    else
        // iOS is < 4.0 
        UIGraphicsBeginImageContext(size);

    // optional: add a shadow, to avoid clipping the shadow you should make the context size bigger 
    //
    // CGContextRef ctx = UIGraphicsGetCurrentContext();
    // CGContextSetShadowWithColor(ctx, CGSizeMake(1.0, 1.0), 5.0, [[UIColor grayColor] CGColor]);

    // draw in context, you can use also drawInRect:withFont:
    [text drawAtPoint:CGPointMake(0.0, 0.0) withFont:font];

    // transfer image
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();    

    return image;
}


calling by:
-----------

UIImage *image = [self imageFromText:@"This is a text"];
link
14_east's gravatar image

14_east

This is not my code but it is my first reaction to your above post.

Put the two images together -

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
- (UIImage *)addImage:(UIImage *)image1 toImage:(UIImage *)image2 {  
    UIGraphicsBeginImageContext(image1.size);  
  
    // Draw image1  
    [image1 drawInRect:CGRectMake(0, 0, image1.size.width, image1.size.height)];  
  
    // Draw image2  
    [image2 drawInRect:CGRectMake(0, 0, image2.size.width, image2.size.height)];  
  
    UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();  
  
    UIGraphicsEndImageContext();  
  
    return resultingImage;  
}


I have also been looking at the difference between UIGraphicsBeginImageContext vs CGBitmapContextCreate.

To get an image (and I am talking in theory) cause i have not set up a test for you I like this idea below better.

1:
2:
3:
4:
5:
6:
7:
8:
9:
+ (UIImage *) imageFromView: (UIView *) theView
{
	UIGraphicsBeginImageContext(theView.frame.size);
	CGContextRef context = UIGraphicsGetCurrentContext();
	[theView.layer renderInContext:context];
	UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
	UIGraphicsEndImageContext();
	return theImage;
}


Thus following from the above example try this for your specific area.  Now we should be limiting it to the position and size of your drawing area but we are getting everything as we are using the layer of self

1:
2:
3:
4:
5:
6:
CGRect contextRect  = CGRectMake(50, 50, 200, 200);// whatever you need
	UIGraphicsBeginImageContext(contextRect.size);	

	[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
	UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
	UIGraphicsEndImageContext();

link
darbid73's gravatar image

darbid73

I'm not quite sure on where to put that bit of code... where were you thinking?
link
14_east's gravatar image

14_east

This bit of code should be able to create your image from the drawing area.
link
darbid73's gravatar image

darbid73

Your answer
[hide preview]

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Tags:

×3
×21
×23
×42
×9

Asked: 07/30/2011 03:39

Seen: 468 times

Last updated: 10/21/2011 03:10