Writing text in png image - ios

Hopefully it will be a duplicate question, but I am really stuck in writing into png data chunks. Can someone help me how to get headers from data, in objective-C? Here is the information Section 4.7.2 , c part, Textual Information.
Thanks

I think there is not need to care about png data chunks, just have a try with the code below:
//load target image, then draw text on the target image
UIImage * image = [UIImage imageNamed:#"targetImage.png"];
NSString * text = #"Watermark!";
CGPoint startPoint = CGPointMake(20, 30);
UIFont *font = [UIFont boldSystemFontOfSize:12];
UIGraphicsBeginImageContext(image.size);
[image drawInRect:CGRectMake(0,0,image.size.width,image.size.height)];
CGRect rect = CGRectMake(startPoint.x, startPoint.y, image.size.width, image.size.height);
[[UIColor whiteColor] set];
[text drawInRect:CGRectIntegral(rect) withFont:font];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// Convert UIImage object into NSData (a wrapper for a stream of bytes) formatted according to PNG spec
NSData *imageData = UIImagePNGRepresentation(newImage);
//Save imageDate to disk as a png file
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *imageSubdirectory = [documentsDirectory stringByAppendingPathComponent:#"watermarkFolderName"];
NSString *filePath = [imageSubdirectory stringByAppendingPathComponent:#"watermark.png"];
[imageData writeToFile:filePath atomically:YES];

Related

Create PDF of A4 size paper with images of original sizes

I am converting each PPT slide into images and creating a PDF from them. Images are created properly with their original sizes. Now to create PDF I am using below code.
CGSize paperSize = CGSizeMake(595.2,841.8);
NSString *strPath = [[documentsPaths objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:#"slide%i.png", i]];
NSData *imgData = [[NSData alloc] initWithContentsOfURL:[NSURL fileURLWithPath:strPath]];
UIImage *image = [[UIImage alloc] initWithData:imgData];
CGRect rect = CGRectMake(0, 0,paperSize.width ,paperSize.height);
UIGraphicsBeginPDFPageWithInfo(rect, nil);
[image drawInRect:rect];
UIGraphicsEndPDFContext();
What it exactly does is, drawing image with paper size and not it's original size. I want to draw image with original size on A4 size paper. Any help will be appreciated.
Kriti's suggestion worked for me. Below is working code:
UIGraphicsBeginPDFContextToData(pdfFile, CGRectMake(0, 0, kPaperSizeA4.width, kPaperSizeA4.height), nil);
NSString *strPath = [[[appDelegate getDocumentDirectory] stringByAppendingPathComponent:#"EPC"] stringByAppendingPathComponent:[NSString stringWithFormat:#"slide%i.png", i]];
NSData *imgData = [[NSData alloc] initWithContentsOfURL:[NSURL fileURLWithPath:strPath]];
image = [[UIImage alloc] initWithData:imgData];
rect = CGRectMake((paperSize.width - image.size.width)/2, (paperSize.height - image.size.height)/2 ,image.size.width ,image.size.height);
UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, kPaperSizeA4.width, kPaperSizeA4.height), nil);
[image drawInRect:rect];
UIGraphicsEndPDFContext();

Converting UILabel into UIImage giving wrong size and poor quality

I have a UILabel of size 180 x 180 and I am converting it in the image, but when I am saving it I am getting 540 x 540 image resolution, I need 180 x 180. Here is my code.
- (void)grabImage {
// Create a "canvas" (image context) to draw in.
UIGraphicsBeginImageContextWithOptions(_lbl1.bounds.size, _lbl1.opaque, 0.0); // high res
// Make the CALayer to draw in our "canvas".
[[_lbl1 layer] renderInContext: UIGraphicsGetCurrentContext()];
// Fetch an UIImage of our "canvas".
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
// Stop the "canvas" from accepting any input.
UIGraphicsEndImageContext();
NSData *pngData = UIImagePNGRepresentation(image);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0]; //Get the docs directory
NSString *filePath = [documentsPath stringByAppendingPathComponent:#"image180.png"]; //Add the file name
[pngData writeToFile:filePath atomically:YES]; //Write the file
}
The quality is poor, in attached image upper one is original UILable and at bottom is the generated image, see the difference

IOS save image in document folder with set dimanation (width and height )

get image from uimageview and save in app document folder its done but the problem is when image is so big resolution then how to set fixed width and height image here is my code please help me
NSInteger N=100000000;
NSUInteger r = arc4random_uniform(N) + 1;
NSString *myimageName=[NSString stringWithFormat:#"image%lu.png",(unsigned long)r];
NSData *pngData = UIImagePNGRepresentation(self.myImage.image ) ;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0]; //Get the docs directory
NSString *filePath = [documentsPath stringByAppendingPathComponent:myimageName]; //Add the file name
[pngData writeToFile:filePath atomically:YES];
+ (UIImage*)imageWithImage:(UIImage*)image
scaledToSize:(CGSize)newSize;
{
UIGraphicsBeginImageContext( newSize );
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
At first resize your image, after save you result.

UIImageJPEGRepresentation taking huge memory

i'm trying to figure out this problem but failed after doing every thing i found on OS or google. Problem is that when i convert UIImage to NSData using UIImageJPEGRepresentation or UIImagePNGRepresentation it increases the memory size to 30Mb (believe me or not).
Here is my code
myImage= image;
LoginSinglton*loginObj = [LoginSinglton sharedInstance];
NSError *error;
NSData *pngData = UIImageJPEGRepresentation(image, scaleValue); //scaleVale is 1.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0]; //Get the docs directory
self.imageCurrentDateAndTime =[self getTimeAndDate];
self.filePathAndDirectory = [documentsPath stringByAppendingPathComponent:#"Photos Dir"];
NSLog(#"Documents path %#",self.filePathAndDirectory);
if (![[NSFileManager defaultManager] createDirectoryAtPath:self.filePathAndDirectory
withIntermediateDirectories:NO
attributes:nil
error:&error])
{
NSLog(#"Create directory error: %#", error);
}
self.imageName= [NSString stringWithFormat:#"photo-%#-%#.jpg",loginObj.userWebID,self.imageCurrentDateAndTime];
NSString *filePath = [self.filePathAndDirectory stringByAppendingPathComponent:self.imageName];
[pngData writeToFile:filePath atomically:YES]; //Write the file
[self writeImageThumbnailToFolder:image];
[self writeImageHomeViewThumbnailToFolder:image];
I have tried following solution as well
UIImageJPEGRepresentation - memory release issue
1- Used #autoreleasepool
2- done pngData = nil;
but still facing that memory issue.
EDIT I think i'm not able to convey my problem. It's ok if UIImageJPEGRepresentation taking huge memory,but memory should back to it's earlier position after saving that image. Hope this will help you in detail.
Use a scaleValue of less than 1. Even 0.9 will massively reduce the memory footprint with minimal quality loss.
Try this:
UIGraphicsBeginImageContext(newSize);
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
If not got solution for your expectation, no need to worry try this too:
UIImage *small = [UIImage imageWithCGImage:original.CGImage scale:0.25 orientation:original.imageOrientation];
Get sample app form here for editing image or scale:
http://mattgemmell.com/2010/07/05/mgimageutilities/
To resize using minimum memory try Using CoreGraphics
SO answer by #Mina Nabil, see full answer for more details
#import <ImageIO/ImageIO.h>
-(UIImage*) resizedImageToRect:(CGRect) thumbRect {
CGImageRef imageRef = [inImage CGImage];
CGImageAlphaInfo alphaInfo = CGImageGetAlphaInfo(imageRef);
if (alphaInfo == kCGImageAlphaNone)
alphaInfo = kCGImageAlphaNoneSkipLast;
// Build a bitmap context that's the size of the thumbRect
CGContextRef bitmap = CGBitmapContextCreate(
NULL,
thumbRect.size.width, // width
thumbRect.size.height, // height
CGImageGetBitsPerComponent(imageRef), // really needs to always be 8
4 * thumbRect.size.width, // rowbytes
CGImageGetColorSpace(imageRef),
alphaInfo
);
// Draw into the context, this scales the image
CGContextDrawImage(bitmap, thumbRect, imageRef);
// Get an image from the context and a UIImage
CGImageRef ref = CGBitmapContextCreateImage(bitmap);
UIImage* result = [UIImage imageWithCGImage:ref];
CGContextRelease(bitmap); // ok if NULL
CGImageRelease(ref);
return result;
}

Save two overlapping images in one, iOS

Possible duplicate : Saving two Overlapping UIImage
In a UIScrollView, i've added my UIImageView and there's a frame Layer above it. I want to save the final Image on screen after all editing. Answer given to the above mentioned question does the work but as it's drawing the frame, results in degradation of quality of my image , So i'm searching for a solution which keeps my image in good resolution.
Please help me out of this, thanks in advance !
Try with this code:
//merge two images for this code
UIImage *bottomImage =imgview.image; //background image ////1st image
UIImage *image = imgProfile.image; //foreground image///2nd image
CGSize newSize = CGSizeMake(270, 330); // set your image rect
UIGraphicsBeginImageContext( newSize );
// Use existing opacity as is
[bottomImage drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];///1st image set frame
// Apply supplied opacity if applicable
[image drawInRect:CGRectMake(81,218,97,78) blendMode:kCGBlendModeNormal alpha:1]; //2nd image set frame on bottom image with alpha value
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:#"savedImage.png"];
NSData *imageData = UIImagePNGRepresentation(newImage);
[imageData writeToFile:savedImagePath atomically:NO];
You can see your newly created image in your application's document directory.

Resources