Add text to UIImage with CoreGraphics - cocoa-touch

I just have to add a green text to UIImage I get from the database, how is it done? Here is what I have so far, something is missing for sure:
UIImage *img=[UIImage imageWithData:data];
UIGraphicsBeginImageContext(CGSizeMake(360, 270));
CGImageRef imageCon=[img CGImage];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSelectFont(context, "Helvetica", 12, NSMacOSRomanStringEncoding);
CGContextSetTextDrawingMode(context, kCGTextFill);
CGContextSetFillColor(context, CGColorGetComponents([[UIColor whiteColor] CGColor]));
CGAffineTransform xform = CGAffineTransformMake( 1.0, 0.0, 0.0, -1.0, 0.0, 0.0);
CGContextSetTextMatrix(context, xform);
CGContextShowTextAtPoint(context, 100.0f, 180.0f, "test", strlen("test"));
UIImage *imgToAdd=[UIImage imageWithCGImage:CGBitmapContextCreateImage(context)];
[arr addObject:imgToAdd];

Related

Rendering PDF in iOS using drawRect and drawLayerInContext

I'm creating a custom view that renders a PDF to its bounds rectangle .
I have pasted the code below :
CGContextSetRGBFillColor(context, 1.0,1.0,1.0,1.0);
CGContextFillRect(context, self.bounds);
CGRect pageRect = CGPDFPageGetBoxRect(page, kCGPDFMediaBox);
CGFloat scale = MIN(self.bounds.size.width / pageRect.size.width, self.bounds.size.height / pageRect.size.height);
CGContextSaveGState(context);
CGContextTranslateCTM(context, 0.0, self.bounds.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextScaleCTM(context, scale, scale);
CGContextDrawPDFPage(context, self.page);
CGContextRestoreGState(context);
My question is , if I use the above code in the drawRect method it just gives me a black rectangle . If I use it in the drawLayer: inContext: method it works fine . What could be the reason ?
PS : When using it drawRect I use the method UIGraphicsGetCurrentContext() to get the current graphics context .
This code actually works fine for me ..
Add breakpoint and check it hits the -drawrect method
CGContextRef context = UIGraphicsGetCurrentContext();
NSString *filePath = [[NSBundle mainBundle]
pathForResource:#"Trial" ofType:#"pdf"];
NSURL *url = [[NSURL alloc] initFileURLWithPath:filePath];
CGPDFDocumentRef document = [self openDocument:(CFURLRef)url];
[url release];
CGPDFPageRef docPageRef =CGPDFDocumentGetPage(document, 1);
CGContextSetRGBFillColor(context, 1.0,1.0,1.0,1.0);
CGContextFillRect(context, self.bounds);
CGRect pageRect = CGPDFPageGetBoxRect(docPageRef, kCGPDFMediaBox);
CGFloat scale = MIN(self.bounds.size.width / pageRect.size.width, self.bounds.size.height / pageRect.size.height);
CGContextSaveGState(context);
CGContextTranslateCTM(context, 0.0, self.bounds.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextScaleCTM(context, scale, scale);
CGContextDrawPDFPage(context, docPageRef);
CGContextRestoreGState(context);
I used this in my drawrect and it worked fine.
Check all the references are getting the value that you are providing to the drawrect
check this

objective-c - Create an UIImage with Core Graphics

I have this code and it's not adding anything to the view, can you help me figure out what is wrong?
also I will apply a filer to image, but that will be later, when I get this done, but tell me if this is a wrong approach if Im going to add filters.
UIGraphicsPushContext(context);
CALayer *layer = [CALayer layer];
CGColorRef bgColor = [UIColor colorWithHue:0.6 saturation:1.0 brightness:1.0 alpha:1.0].CGColor;
layer.frame = CGRectMake(0, 0, 200, 200);
CGContextSetFillColorWithColor(context, bgColor);
CGContextFillRect(context, layer.bounds);
CGContextSaveGState(context);
CGColorSpaceRef patternSpace = CGColorSpaceCreatePattern(NULL);
CGContextSetFillColorSpace(context, patternSpace);
CGColorSpaceRelease(patternSpace);
CGContextFillRect(context, layer.bounds);
CGContextRestoreGState(context);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageView *borrar = [[UIImageView alloc]initWithImage:image];
[self.view addSubview:borrar];
You never open an image context. Use UIGraphicsBeginImageContextWithOptions.

Slightly-transparent white line around UIImage after adding overlays?

I have a function that will overlay images over another image and return as a UIImage. However, a white line-ish thing comes up on it. Here's what I have
- (UIImage * ) romanticFilterImage: (UIImage *) imageA {
UIImage *vintageOne = [UIImage imageNamed:#"filter_vintage_1.png"];
UIImage *vintageTwo= [UIImage imageNamed:#"filter_vintage_2.png"];
UIImage *vintageThree = [UIImage imageNamed:#"filter_vintage_3.png"];
UIGraphicsBeginImageContextWithOptions(CGSizeMake(imageA.size.width, imageA.size.height), YES, 0.0);
[imageA drawAtPoint: CGPointMake(0,0)];
[vintageThree drawInRect:CGRectMake(0, 0, imageA.size.width, imageA.size.height) blendMode: kCGBlendModeMultiply alpha: 1.0];
[vintageTwo drawInRect:CGRectMake(0, 0, imageA.size.width + 2, imageA.size.height + 2) blendMode: kCGBlendModeColorBurn alpha: 1.0];
[vintageOne drawInRect:CGRectMake(0, 0, imageA.size.width, imageA.size.height) blendMode: kCGBlendModeMultiply alpha: 1.0];
UIImage *source = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGSize size = [source size];
UIGraphicsBeginImageContext(size);
CGRect rect = CGRectMake(0, 0, size.width, size.height);
[source drawInRect:rect blendMode:kCGBlendModeNormal alpha:1.0];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetRGBStrokeColor(context, 5.0, 5.0, 5.0, 5.0);
CGContextStrokeRect(context, rect);
UIImage *testImg = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return testImg;
}
Here is a screenshot: (I have added a black background to enhance the slighly transparent border.
Aren't you stroking the image white in these lines?
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetRGBStrokeColor(context, 5.0, 5.0, 5.0, 5.0);
CGContextStrokeRect(context, rect);
UIImage *testImg = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Although I am not sure if you intended GContextSetRGBStrokeColor(context, 5.0, 5.0, 5.0, 5.0); as 1.0 is the maximum value for the rgba values in the function.

Objective C - remove drawing

I've been using the code below to draw on an UIView
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
CGContextSetRGBFillColor(context, 0.0, 0.0, 1.0, 1.0);
CGContextSetAlpha(context, 0.5);
CGContextSetLineWidth(context, 10.0);
CGContextMoveToPoint(context, point.x, point.y);
CGContextAddLineToPoint(context, point.x, point.y);
CGContextStrokePath(context);
So that part works.
Now how do I clear my drawing? Most of the example just shows the drawing. Having a hardtime finding keyword to google.
Thanks,
Tee
Have you tried CGContextClearRect?
[self setNeedsDisplay];
it sends the message drawRect: to the view.

Why my image is up side down?

This is my drawInContext method:
UIImage *img = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:#"myImage" ofType:#"png"]];
CGRect cropRect = CGRectMake(0, 0, 175, 175);
CGImageRef imageRef = CGImageCreateWithImageInRect([img CGImage], cropRect);
CGRect imageRect;
imageRect.origin = CGPointMake(0, 0);
imageRect.size = CGSizeMake(175, 175);
CGContextDrawImage(UIGraphicsGetCurrentContext(), imageRect, imageRef);
CGImageRelease(imageRef);
My image is upside down, how can I change it? What's wrong with my code? How can I change the image become normal?? thx .
Instead of this:
CGContextDrawImage(UIGraphicsGetCurrentContext(), imageRect, imageRef);
do this:
[img drawInRect:CGRectMake(0, 0, 175, 175)]
CGContextDrawImage doesn't preserve orientation.