Add Text to the Image - iphone

HI
Im currently developing an application where i have to add the text over the image at any position in the image(not subview) and the output should be the single image file with the original image and the text embedded in it,any help will be appreciable.
eg : water mark on the image
Thanks
sivasankar

UIImage *myImage = loadUnwatermarkedImage();
NSString *myWatermarkText = #"Watermark";
UIImage *watermarkedImage = nil;
UIGraphicsBeginImageContext(myImage.size);
[myImage drawAtPoint: CGPointZero];
[myWatermarkText drawAtPoint: CGPointMake(10, 10) withFont: [UIFont systemFontOfSize: 12]];
watermarkedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Upon completion the watermarkedImage will be an autoreleased watermarked image. loadUnwatermarkedImage() is a fictional function providing an original image.

Related

save an image with two UIImageView which has two images - ios [duplicate]

This question already has answers here:
How can i add two uiimageview and save it to iphone photo gallery in iphone application development?
(2 answers)
Closed 9 years ago.
In my application I have two UIImageViews which is overlayed one over the other. i.e ) UIImageView-2 is placed on UIImageView-1. The image in UIImageView-2 is transparent, so now I have image over another image. Now I need to save these two as a single image. Is it possible in ios? Please advice..
The sample view is as below: The red mark is an image in a view over tennis ball image view.
Hope following method will help you.
-(CGImageRef )mergedImageFromImageOne:(UIImage *)imageOne andImageTwo:(UIImage *)imageTwo
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
CGSize imageSize = imageOne.size;
UIGraphicsBeginImageContext(sizeVideo);
[imageOne drawInRect:CGRectMake(0,0,imageSize.width,imageSize.height)];
[imageTwo drawInRect:CGRectMake(0,0,imageSize.width,imageSize.height) alpha:1];
CGImageRef imageRefNew = CGImageCreateWithImageInRect(UIGraphicsGetImageFromCurrentImageContext().CGImage, CGRectMake(0,0,imageOne.width,imageOne.height));
UIGraphicsEndImageContext();
[pool release];
return imageRefNew;
}
I've taken one Logo Image which is set on main image as application ref.
So you can refer this, try with your images.
set width(w) & height(h) as per your image.
Try this ::
UIImage *img_Logo = [UIImage imageNamed:#"Img_Logo.png"];
CGSize newSize = CGSizeMake(w, h);
UIGraphicsBeginImageContext(newSize);
// Use existing opacity as is
[main_image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
// Apply supplied opacity
[img_Logo drawInRect:CGRectMake(0,0,newSize.width,newSize.height) blendMode:kCGBlendModeNormal alpha:0.8];
main_image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
imgView.image = main_image;
Hope, this'll help you.
Thanks.
Why you are not taking screenshot for only that portion??

iphone: Scale the image in table cell

I am using a UITableViewCellStyleSubTitle. I am trying to add thumbnails to my table cells.
UIViewContentModeScaleAspectFit does not seem to work for reasons unknown. I have seen a lot of answers in stack overflow for similar kind of questions. If i crop the image using drawInRect it works, like this
[thumbnail drawInRect:thumbnailRect];
UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
But what i am trying to achieve should not need me to do this. I am only trying to scale the image and not crop it.
So setting the content mode to "UIViewContentModeScaleAspectFit" for the imageview of the default cell should work.. but it does not.
cell.imageView.contentMode = UIViewContentModeScaleAspectFit;
Complete logic for setting thumbnails.. fPath is the path to image
UIImage *thumbnail = [UIImage imageWithContentsOfFile:fPath];
[cell.imageView setImage:thumbnail];
cell.imageView.contentMode = UIViewContentModeScaleAspectFit;
cell.imageView.clipsToBounds = YES;
I found out that drawInRect stretches the original image if the size is lower or croppes it if it is bigger.
This article will help you to prepare a thumbnail of your image
http://vocaro.com/trevor/blog/2009/10/12/resize-a-uiimage-the-right-way/
It has several useful UIImage classes extentions:
* UIImage+Resize
* UIImage+Alpha
* UIImage+RoundedCorner
Example Usage:
UIImage *imageThumb = [YourImage thumbnailImage:70 transparentBorder:0 cornerRadius:10 interpolationQuality:kCGInterpolationHigh];
and then you can use drawInRect method. If the image is not rectangle - the image will crop the bigger side and take the middle of the image
Hope this answer will guide you.

How do I modify my code to allow user to position a UIImage on a UIImageView?

I have the following code where I am hard coding a UImage on a UIImageView. However I have been asked to modify so that the application allows user to select where on the UIImageView, the UIImage needs to be placed at. I am thinking the user clicks on the screen and the application stores the x,y coordinates where I place the UIImage. I just don't know how to get about doing that.
CGPDFDocumentRef document = CGPDFDocumentCreateWithURL((CFURLRef)pdfUrl);
//lets now convert the fax to an image
CGPDFPageRef page = CGPDFDocumentGetPage(document, 1);
UIImage *pageImage = [PDFPageConverter convertPDFPageToImage:page withResolution:144];
UIImage *shieldLogo = [UIImage imageNamed:#"shield_bw.gif"];
UIGraphicsBeginImageContext(pageImage.size);
[pageImage drawInRect:CGRectMake(0, 0, pageImage.size.width, pageImage.size.height)];
[shieldLogo drawInRect:CGRectMake(150, 100, shieldLogo.size.width, shieldLogo.size.height)];
[theSignature drawAtPoint:CGPointMake(200, 100) withFont:[UIFont fontWithName:#"Arial" size:15.0]];
[theSignature2 drawAtPoint:CGPointMake(200, 120) withFont:[UIFont fontWithName:#"Arial" size:15.0]];
UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[image setImage:resultingImage];
Make your subclass responding to touch events using -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;, in which you will modify the frame property of the image view accordingly.
You can also use UIGestureRecognizers in order to alter the UIImage's position.
This is the tutorial that I used to learn how and does a better job than I can here.
http://www.icodeblog.com/2010/10/14/working-with-uigesturerecognizers/?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+icodeblog+%28iCodeBlog%29
Good luck!

How to create a new image from an existing image and a string over it

I need to create something like:
http://t3.gstatic.com/images?q=tbn:ANd9GcQZu06WspVUaFTn-C-YD6Pzu_oWH5NLA1Q8nPUZEdemllFIAMNn
So I need to place a user input string over an image in a specified spot, then save it in the memory.
How can I do it?
Create a new bitmap image context. See this for help creating-and-drawing-on-a-new-uiimage
Draw your UIImage in drawInRect
Create black, rounded and transparent image. See this for help:
resize-a-uiimage-the-right-way
Draw black image drawInRect
Draw your NSString text. see this for help how-to-display-text-using-quartz-on-the-iphone drawInRect or drawInPoint
Here is a small code example for cropping an image.
- (UIImage *)croppedImageWithRect:(CGRect)bounds {
CGImageRef imageRef = CGImageCreateWithImageInRect(scaleImage.CGImage, bounds);
UIImage *croppedImage = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
return croppedImage;
}
Enjoy.
use a UItextView over an image to show such string.

Is this kind of masking possible with UIImage or CGImage API in iOS

I have an UIImage with some text and would like to apply pattern UIImage as masking. Is this possible ?
I understand that with UILabel we can get this kind of gradient using CAGradientLayer. But can this be done if the source is an UIImage ?
The image may have some symbols/pictures etc other than regular characters and hence UIImage. Also i could reuse the image by applying different masking pattern depending on the context.
is this possible ?
Appreciate your help.
EDIT: Thanks for all your answers.
I understand applying the gradient to a text label or creating an image that has text.
But my goal is to get this.--> Click here
i.e. i have a png with some drawing like a flower with transparent background. I want to apply the gradient to the object inside that picture at runtime with a gradient.png as shown in the picture above. Is that possible with masking ?
Thanks
Looks like you should be able to use CGImageMaskCreate:
- (UIImage*) maskImage:(UIImage *)image withMask:(UIImage *)maskImage {
CGImageRef maskRef = maskImage.CGImage;
CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(maskRef),
CGImageGetHeight(maskRef),
CGImageGetBitsPerComponent(maskRef),
CGImageGetBitsPerPixel(maskRef),
CGImageGetBytesPerRow(maskRef),
CGImageGetDataProvider(maskRef), NULL, false);
CGImageRef masked = CGImageCreateWithMask([image CGImage], mask);
return [UIImage imageWithCGImage:masked];
}
For a longer discussion check out the comment thread here:
http://iosdevelopertips.com/cocoa/how-to-mask-an-image.html
Yes, it is :)
textField.textColor = [UIColor colorWithPatternImage: [UIImage imageNamed:#"rainbowGradient.png"]];
If you want heavy control, Jason Whyne's idea might work. But I like this one, because it's about 8 lines shorter.
Here's just another way to draw an image of text masking something. It's based on kCGBlendModeSourceIn blending mode: you draw text on a clear background and then draw the fill all over the place.
NSString *theString = ...;
UIFont *theFont = ...;
CGSize stringSize = [theString sizeWithFont:theFont];
// The background must be clear (fully transparent), hence NO as the 2nd argument
UIGraphicsBeginImageContextWithOptions(stringSize, NO, 0);
[theString drawAtPoint:CGPointZero withFont:theFont];
// This effectively colorizes the image. Use a pattern color...
[patternColor set];
UIRectFillUsingBlendMode(CGRectMake(0, 0, stringSize.width, stringSize.height), kCGBlendModeSourceIn);
// ... or an image:
[patternImage drawInRect:CGRectMake(0, 0, stringSize.width, stringSize.height) blendMode:kCGBlendModeSourceIn alpha:1.0f];
UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
sample using mask is fine and works well, but You are leaking.
CGImageMaskCreate and CGImageCreateWithMask do allocate (following "create" -> retain rule)
so You should release mask & image after using:
CGImageRef masked = CGImageCreateWithMask([image CGImage], mask);
UIImage * result = [UIImage imageWithCGImage:masked];
CGImageRelease(mask);
CGImageRelease(masked);
return result;
As per ADC docs:
...
Return Value
A Quartz bitmap image mask. You are responsible for releasing this object by calling CGImageRelease.