I have the follwoing code to save the image but it seems poor in quality...
is there any way to get the images in a fine quality?
Thanks for any help
UIGraphicsBeginImageContext(CGSizeMake(self.view.bounds.size.width,
self.view.bounds.size.height-50));
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(context, 0, -50);
[self.view.layer renderInContext:context];
UIImage* viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext( );
NSData* imageData = [NSData dataWithData:UIImageJPEGRepresentation( viewImage,0 )];
First thing that comes to mind is a compression quality parameter in UIImageJPEGRepresentation function - you set it to the lowest quality (0.0), try to set it to maximum quality value:
NSData* imageData = [NSData dataWithData:UIImageJPEGRepresentation( viewImage,1.0 )];
One option as it is mentioned in Vladimir's answer. Another option is you can use the UIImagePNGRepresentation. It will use the PNG format.
Related
I am trying to generate a PDF report from a UIWebView. Using Graphics Contexts I can get the images of screen frame and draw it in PDF.
Problems in curent method are,
Images are blurred (bad quality)
So the report pdf is not readable.
How can I get a high quality images of a screen frame? Or is there any other better solutions for my problem?
P.S. Report includes multiple background colors and images, I think it is hard to directly draw them in PDF.
+ (UIImage *) imageWithView:(UIView *)view
{
UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
NSMutableData *pdfData = [NSMutableData data];
UIGraphicsBeginPDFContextToData(pdfData, webview.bounds, nil);
UIGraphicsBeginPDFPage();
CGContextRef pdfContext = UIGraphicsGetCurrentContext();
[webview.layer renderInContext:pdfContext];
UIGraphicsEndPDFContext();
Have you tried like this?
Can anyone suggest methods to enhance the quality of an image, being resized to a larger size?
On resizing, the resultant image pixelates. I need this pixelation to reduce to maximum extent.
You can use the following code for compressing or increasing size of an image.
CGFloat compression = 222.0f;
CGFloat maxCompression = 202.1f;
int maxFileSize = 160*165; //fill your size need
NSData *imageDat = UIImageJPEGRepresentation(image.image, compression);
while ([imageDat length] > maxFileSize && compression > maxCompression)
{
compression -= 0.1222;
imageDat = UIImageJPEGRepresentation(decodedimage.image, compression);
}
NSLog(#"image compressed success");
[image setImage:[UIImage imageWithData:imageDat]];
You can use the following code for compressing or increasing size of an image.
image1 = [self imageWithImage:image1 convertToSize:CGSizeMake(100, 100)];
// For convert the image to fix size
- (UIImage *)imageWithImage:(UIImage *)image convertToSize:(CGSize)size
{
UIGraphicsBeginImageContext(size);
[image drawInRect:CGRectMake(0, 0, 320,460)];
UIImage *destImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return destImage;
}
There is an Article called : Resize a UIImage the right way , you should Read and Understand each and every aspects given there. I think it will give Answers of Every Questions arises in your mind. I think it's a beautiful Article with proper Graphical Representation (means Less Boring at Reading).
i need to capture image by using UIGraphicsBeginImageContextWithOptions my requirement is to start capturing from -20 from origin
any help will be highly appreciated
Use following code and Import <QuartzCore/QuartzCore.h>. You can also use this code after capturing image. So you can remove unnecessary part from that.
UIGraphicsBeginImageContext(self.view.frame.size);
//or
UIGraphicsBeginImageContextWithOptions(self.view.frame.size, NO, 0.0);//this will also give you scaling option and more better for resolution.
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
CGImageRef ref = viewImage.CGImage;
CGRect myImageArea = CGRectMake (20, 20,
300, 100);// you define coordinates according your requirements.
CGImageRef mySubimage = CGImageCreateWithImageInRect (ref, myImageArea);
UIImage *yourfinalcapturedimage = [UIImage imageWithCGImage:mySubimage];// this will give you your required image.
Hope, this will help you..
I m taking images from photo library.I have large images of 4-5 mb but i want to compress those images.As i need to store those images in local memory of iphone.for using less memory or for getting less memory warning i need to compress those images.
I don't know how to compress images and videos.So i want to know hot to compress images?
UIImage *image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
NSData* data = UIImageJPEGRepresentation(image,1.0);
NSLog(#"found an image");
NSString *path = [destinationPath stringByAppendingPathComponent:[NSString stringWithFormat:#"%#.jpeg", name]];
[data writeToFile:path atomically:YES];
This is the code for saving my image. I dont want to store the whole image as its too big. So, I want to compress it to a much smaller size as I'll need to attach multiple images.
Thanks for the reply.
You can choose a lower quality for JPEG encoding
NSData* data = UIImageJPEGRepresentation(image, 0.8);
Something like 0.8 shouldn't be too noticeable, and should really improve file sizes.
On top of this, look into resizing the image before making the JPEG representation, using a method like this:
+ (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {
UIGraphicsBeginImageContext(newSize);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
Source: The simplest way to resize an UIImage?
UIImageJPEGRepresentation(UIImage,Quality);
1.0 means maximum Quality and 0 means minimum quality.
SO change the quality parameter in below line to reduce file size of the image
NSData* data = UIImageJPEGRepresentation(image,1.0);
NSData *UIImageJPEGRepresentation(UIImage *image, CGFloat compressionQuality);
OR
NSData *image_Data=UIImageJPEGRepresentation(image_Name,compressionQuality);
return image as JPEG. May return nil if image has no CGImageRef or invalid bitmap format. compressionQuality is 0(most) & 1(least).
I have a little sketch program that I've managed to throw together using Quartz2D, but I'm very new to the iOS platform and I'm having trouble figuring out how to allow the user to save their sketch. I have a CGContextRef that contains the sketch, how can I save it so that it can later be retrieved? Once it's saved, how can I retrieve it?
Thanks so much in advance for your help! I'm going to continue reading up on this now...
There are different types of CGContexts. You are most likely having a screen based context, but you would need either a bitmapContext or a pdfContext, to create a bitmap or a pdf file.
See the UIKit Functions Reference. Functions that should be of interest are:
UIGraphicsBeginImageContext()
UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
UIImageJPEGRepresentation()
UIImageWriteToSavedPhotosAlbum()
UIGraphicsBeginPDFContextToFile()
UIGraphicsEndPDFContext()
Here's how I did it...
Saving
CGImageRef imageRef = CGBitmapContextCreateImage(bitmapContext);
UIImage* image = [[UIImage alloc] initWithCGImage:imageRef];
NSData* imageData = UIImagePNGRepresentation(image);
[imageData writeToFile:filePath atomically:YES];
Retrieving
UIImage* image = [UIImage imageWithContentsOfFile:filePath];
CGImageRef imageRef = [image CGImage];