Add text string to photo when saving to camera roll - ios

Is it possible to add a text string to an image when saving to camera roll?
for example photo is taken and saved to camera roll:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
UIImageWriteToSavedPhotosAlbum(image, nil,#selector(image:didFinishSavingWithError:contextInfo:), nil);
//something here to add #"my pre defined text"; to saved image
}
I want to automatically add pre defined text to this saved image without any user input if possible?
Nothing comes up on the usual searches on SO and Google regarding what I am trying to do

Take a Look on my code:
-(UIImage *)addText:(UIImage *)img text:(NSString *)text1
{
int w = img.size.width;
int h = img.size.height;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);
CGContextDrawImage(context, CGRectMake(0, 0, w, h), img.CGImage);
CGContextSetRGBFillColor(context, 0.0, 0.0, 1.0, 1);
char* text = (char *)[text1 cStringUsingEncoding:NSASCIIStringEncoding];
CGContextSelectFont(context, "Arial", 18, kCGEncodingMacRoman);
CGContextSetTextDrawingMode(context, kCGTextFill);
CGContextSetRGBFillColor(context, 255, 255, 255, 2);
CGContextShowTextAtPoint(context, 10, 170, text, strlen(text));
CGImageRef imageMasked = CGBitmapContextCreateImage(context);
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
return [UIImage imageWithCGImage:imageMasked];
}
And improve with:
- (IBAction)drawImage:(id)sender {
NSString *text1 = myTextField.text;
UIImage *updatedImg = [self addText:myImageView.image text:text1];
[myImageView setImage: updatedImg];
}
I hope this help you ;)
thx for vote ;)

This question appears to address your needs:
Add Text to UIImage
Perhaps ignore the camera action, and the camera roll, what you really just want to do is add a string to a UIImage.

Related

high memory usage when resizing uiimage

Heavy memory usage when resizing and drawing UIImage in rect.
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *selectedImage=[info objectForKey:UIImagePickerControllerOriginalImage];
// COMPRESSING IMAGE
NSData *selectedImageData=UIImageJPEGRepresentation(selectedImage, 0.5);
UIImage *selectedImageFromData=[UIImage imageWithData:selectedImageData];
//SCALING UIIMAGE
UIImage *scaledImage=[[UIImage alloc]init];
if (((selectedImageFromData.size.width>3264) && (selectedImageFromData.size.height>2448)) || ((selectedImageFromData.size.height>3264) && (selectedImageFromData.size.width>2448)))
{
CGFloat originalWidth=selectedImageFromData.size.width;
CGFloat originalHeight=selectedImageFromData.size.height;
CGFloat myWidth=2048;
CGFloat widthRatio=myWidth/originalWidth;
CGFloat dynamicHeight=widthRatio*originalHeight;
CGImageRef CoreGraphicsImage=selectedImageFromData.CGImage;
CGColorSpaceRef colorSpace = CGImageGetColorSpace(CoreGraphicsImage);
CGBitmapInfo bitmapInfo=CGImageGetBitmapInfo(CoreGraphicsImage);
CGImageGetBitsPerComponent(CoreGraphicsImage);
CGContextRef context=CGBitmapContextCreate(NULL, myWidth, dynamicHeight, CGImageGetBitsPerComponent(CoreGraphicsImage), CGImageGetBytesPerRow(CoreGraphicsImage), colorSpace, bitmapInfo);
CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
CGContextDrawImage(context, CGRectMake(0, 0, myWidth, dynamicHeight), CoreGraphicsImage);
CGImageRef CGscaledImage=CGBitmapContextCreateImage(context);
UIImage *CGLastimage = [UIImage imageWithCGImage: CGscaledImage];
NSLog(#"%f",CGLastimage.size.width);
NSLog(#"%f",CGLastimage.size.height);
VisualEffectImageVIew.image=CGLastimage;
BackgroundImageView.image=CGLastimage;
ForegroundImageView.image=CGLastimage;
}
else
{
NSLog(#" HEIGHT %f",selectedImageFromData.size.height);
NSLog(#" WIDTH %f",selectedImageFromData.size.width);
VisualEffectImageVIew.image=selectedImageFromData;
BackgroundImageView.image=selectedImageFromData;
ForegroundImageView.image=selectedImageFromData;
}
if(UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPhone)
{
[picker dismissViewControllerAnimated:YES completion:nil];
}
else
{
[popoverController dismissPopoverAnimated:YES];
[self popoverControllerDidDismissPopover:popoverController];
}
}
Here's an image below, in the image you can see the above mentioned code is using too much memory. Would like to know why it's using too much memory and would appreciate any solutions.
When it comes to images, i've always been a big fan of Brad Larson's GPUImage, it can do anything you want and uses GPU instead of CPU, which leave you with plenty of power to avoid crashes due to memory pressure.
And also it's pretty good.
This stackoverflow question might also help you achieve what you want once you're done setting up the project.
Hope this helps :)
You're using CPU for drawing data into a frame, i suggest you use CGImage for draw in GPU Context
- (UIImage *) scaleToSize: (CGSize)size
{
// Scalling selected image to targeted size
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(NULL, size.width, size.height, 8, 0, colorSpace, kCGImageAlphaPremultipliedLast);
CGContextClearRect(context, CGRectMake(0, 0, size.width, size.height));
if(self.imageOrientation == UIImageOrientationRight)
{
CGContextRotateCTM(context, -M_PI_2);
CGContextTranslateCTM(context, -size.height, 0.0f);
CGContextDrawImage(context, CGRectMake(0, 0, size.height, size.width), self.CGImage);
}
else
CGContextDrawImage(context, CGRectMake(0, 0, size.width, size.height), self.CGImage);
CGImageRef scaledImage=CGBitmapContextCreateImage(context);
CGColorSpaceRelease(colorSpace);
CGContextRelease(context);
UIImage *image = [UIImage imageWithCGImage: scaledImage];
CGImageRelease(scaledImage);
return image;
}

How to write a text on image iOS?

In my app I'm trying to add a text on an image. I've done it using a post in stackoverflow. It's like the following.
+(UIImage *)addText:(UIImage *)img text:(NSString *)text1{
int w = img.size.width;
int h = img.size.height;
//lon = h - lon;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);
CGContextDrawImage(context, CGRectMake(0, 0, w, h), img.CGImage);
CGContextSetRGBFillColor(context, 0.0, 0.0, 1.0, 1);
char* text = (char *)[text1 cStringUsingEncoding:NSASCIIStringEncoding];// "05/05/09";
CGContextSelectFont(context, "Arial", 18, kCGEncodingMacRoman);
CGContextSetTextDrawingMode(context, kCGTextFill);
CGContextSetRGBFillColor(context, 255, 255, 255, 1);
//rotate text
CGContextSetTextMatrix(context, CGAffineTransformMakeRotation( -M_PI/4 ));
CGContextShowTextAtPoint(context, 4, 52, text, strlen(text));
CGImageRef imageMasked = CGBitmapContextCreateImage(context);
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
return [UIImage imageWithCGImage:imageMasked];
}
When I'm writing a text using this code, the original image's colour is changed like the following,
Original Image
Output with Text
Can anyone explain why is this happening. and how to fix this??
Thanks in Advance!!
We need to create a image context and apply drawing and get final image from this context. You can try this method. Hope this help you.
+ (UIImage *) addText:(UIImage *)img text:(NSString *)text
{
CGRect rect = [[UIScreen mainScreen] bounds];
// create a context according to image size
UIGraphicsBeginImageContext(rect.size);
// draw image
[img drawInRect:rect];
UIFont* font = [UIFont systemFontOfSize:14.0];
/// Make a copy of the default paragraph style
NSMutableParagraphStyle* paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
/// Set line break mode
paragraphStyle.lineBreakMode = NSLineBreakByTruncatingTail;
/// Set text alignment
paragraphStyle.alignment = NSTextAlignmentCenter;
NSDictionary *attributes = #{ NSFontAttributeName: font,
NSParagraphStyleAttributeName: paragraphStyle };
CGRect textRect = CGRectMake(20, 160.0, 280.0, 44);
/// draw text
[text drawInRect:textRect withAttributes:attributes];
// get as image
UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
EDIT: This can only be run on the main thread! So it's pretty much useless.
+(UIImage *)imageFromView:(UIView *)view
{
UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
Here's a new one using Core Graphics that can be used on any thread
+ (UIImage *)imageFromView:(UIView *)view
{
size_t width = view.bounds.size.width*2;
size_t height = view.bounds.size.height*2;
unsigned char *imageBuffer = (unsigned char *)malloc(width*height*8);
CGColorSpaceRef colourSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef imageContext = CGBitmapContextCreate(imageBuffer, width, height, 8, width*4, colourSpace,kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Big);
CGColorSpaceRelease(colourSpace);
CGContextTranslateCTM(imageContext, 0.0, height);
CGContextScaleCTM(imageContext, 2.0, -2.0);
[view.layer renderInContext:imageContext];
CGImageRef outputImage = CGBitmapContextCreateImage(imageContext);
UIImage *image = [[UIImage alloc] initWithCGImage:outputImage];
CGImageRelease(outputImage);
CGContextRelease(imageContext);
free(imageBuffer);
return image;
}
I am using this and it works fine for me
- (UIImage*) drawText:(NSString*) text
inImage:(UIImage*) image
atPoint:(CGPoint) point
{
UIFont *font = [UIFont fontWithName:FONT_BOLD size:20];
UIGraphicsBeginImageContext(image.size);
[image drawInRect:CGRectMake(0,0,image.size.width,image.size.height)];
CGRect rect = CGRectMake(point.x, point.y, image.size.width, image.size.height);
[UIColorFromRGB(0x515151) set];
[text drawInRect:CGRectIntegral(rect) withFont:font];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}

Take photo from camera with date like digital camera in iOS

Need to take photo from the application with date and time the photo has been taken to be displayed in photo just like digital camera.
-(UIImage *)addText:(UIImage *)img text:(NSString *)text1;
{
int w = img.size.width;
int h = img.size.height;
//lon = h - lon;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);
CGContextDrawImage(context, CGRectMake(0, 0, w, h), img.CGImage);
CGContextSetRGBFillColor(context, 0.0, 0.0, 1.0, 1);
char* text = (char *)[text1 cStringUsingEncoding:NSASCIIStringEncoding];// "05/05/09";
CGContextSelectFont(context, "Arial", 18, kCGEncodingMacRoman);
CGContextSetTextDrawingMode(context, kCGTextFill);
CGContextSetRGBFillColor(context, 255, 255, 255, 1);
//rotate text
CGContextSetTextMatrix(context, CGAffineTransformMakeRotation( -M_PI/4 ));
CGContextShowTextAtPoint(context, 4, 52, text, strlen(text));
CGImageRef imageMasked = CGBitmapContextCreateImage(context);
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
return [UIImage imageWithCGImage:imageMasked];
}

How to take screenshot programmatically along with current date printed on it?

I have taken screenshot programmatically for a scroll view in xcode. But the problem i am facing is, i need to take screenshot along with present date in it. Please help me.
Before taking screenshot, put one label with current date, and remove it after taking screenshot.
Or you could use the EXIF meta data to record the date (it's probably already in there). I guess an embedded date in the image is harder to fake, though.
This is how you can add a text to an image. Try this with the date as string. Once you taken the screenshot pass it to the below method and use the output image. Modify the below method for your convenience.
//Add text to UIImage
-(UIImage *)addText:(UIImage *)img text:(NSString *)text1{
int w = img.size.width;
int h = img.size.height;
//lon = h - lon;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);
CGContextDrawImage(context, CGRectMake(0, 0, w, h), img.CGImage);
CGContextSetRGBFillColor(context, 0.0, 0.0, 1.0, 1);
char* text = (char *)[text1 cStringUsingEncoding:NSASCIIStringEncoding];// "05/05/09";
CGContextSelectFont(context, "Arial", 18, kCGEncodingMacRoman);
CGContextSetTextDrawingMode(context, kCGTextFill);
CGContextSetRGBFillColor(context, 255, 255, 255, 1);
//rotate text
CGContextSetTextMatrix(context, CGAffineTransformMakeRotation( -M_PI/4 ));
CGContextShowTextAtPoint(context, 4, 52, text, strlen(text));
CGImageRef imageMasked = CGBitmapContextCreateImage(context);
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
return [UIImage imageWithCGImage:imageMasked];
}
Source

How to change UIBarButtonItem's title color when placed in a UIToolbar?

I've tried setting the title text attributes. The Button is set by using an icon and then setting the title property. The icon is properly tinted. The title remains 'untinted'
Screenshot attached.
If tintColor does not affect the text color, the only solution I know of is to essentially create a composite image of the icon and text, then use that as your button image:
UIGraphicsBeginImageContextWithOptions(someSize, NO, 0);
CGContextRef context = UIGraphicsGetCurrentContext();
// draw the image
// Use UIKit NSString additions to draw the text
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
David's suggestion works. Here is sample code:
+ (UIImage *) image:(NSString *) imageName withTitle:(NSString *)title {
UIFont *titleFont = [UIFont boldSystemFontOfSize:10];
CGSize textSize = [title sizeWithFont:titleFont];
CGFloat width = MAX(35, textSize.width);
UIGraphicsBeginImageContextWithOptions(CGSizeMake(width, 35), NO, 0);
CGContextRef context = UIGraphicsGetCurrentContext();
UIImage *toolbarImage = [UIImage imageNamed:imageName];
CGContextSaveGState(context); {
CGContextTranslateCTM(context, 0, 35);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextDrawImage(context, CGRectMake((width - toolbarImage.size.width) / 2, 14, toolbarImage.size.width, toolbarImage.size.height), toolbarImage.CGImage);
}
CGContextRestoreGState(context);
[title drawInRect:CGRectMake((width - textSize.width) / 2, 22, textSize.width, textSize.height) withFont:titleFont];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
Tweak some of the frame parameters to fit your images.

Resources