When i am using UIImagePNGRepresentation or UIImageJPEGRepresentation for converting UIImage into NSdata, the image size is too much increased.
Steps to Reproduce:
1)Open Xcode and select new project as single view based application
2)Open ViewController.xib and add two buttons named as i)Test Online Image ii)Test Local image
3)Add two IBActions
i) -(IBAction)ClickLocalImageTest:(id)sender;
ii) -(IBAction)ClickOnLineImageTest:(id)sender;
4)Connect "Test Online Image" to "-(IBAction)ClickOnLineImageTest:(id)sender
"
and "Test Local image" to " -(IBAction)ClickLocalImageTest:(id)sender
;"
5)impalement "-(IBAction)ClickLocalImageTest:(id)sender
" method like as following
- (IBAction)ClickLocalImageTest:(id)sender {
NSLog(@"*************Test Local Image****************
");
NSString *path=[[NSBundle mainBundle] pathForResource:@"hero_ipad_retina" ofType:@"jpg"];
NSLog(@"Before testing image size is :<---- %u kb",[[NSData dataWithContentsOfFile:path] length]/1024);
UIImage *img = [UIImage imageNamed:@"hero_ipad_retina.jpg"];
NSLog(@"UIImagePNGRepresentation: image size is---->: %u kb",[UIImagePNGRepresentation(img) length]/1024);
NSLog(@"UIImageJPEGRepresentation with scale 1.0: image size is---->: %u kb
",[UIImageJPEGRepresentation(img, 1.0) length]/1024);
NSLog(@"*************Completed test****************
");
}
6) impalement "- (IBAction)ClickOnLineImageTest:(id)sender
" method as following
- (IBAction)ClickOnLineImageTest:(id)sender {
NSLog(@"*************Test Online Image****************
");
NSLog(@"Before testing image size is :<---- %u kb",[[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://images.apple.com/home/images/hero_ipad_retina.jpg"]] length]/1024);
UIImage *img = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://images.apple.com/home/images/hero_ipad_retina.jpg"]]];
NSLog(@"UIImagePNGRepresentation: image size is---->: %u kb",[UIImagePNGRepresentation(img) length]/1024);
NSLog(@"UIImageJPEGRepresentation with scale 1.0: image size is---->: %u kb
",[UIImageJPEGRepresentation(img, 1.0) length]/1024);
NSLog(@"*************Completed test****************
");
}
7)Please download "hero_ipad_retina.jpg" image from here and save in your resources named as "hero_ipad_retina.jpg"
7)Now run this project on Xcode 4.0 later and IOS3.0 above SDK
**
Expected Results:
1)Click on "Test Online Image" button result should be as following
*************Test Online Image****************
Before testing image size is :<---- 78 kb
UIImagePNGRepresentation: image size is---->: 78 kb
UIImageJPEGRepresentation with scale 1.0: image size is---->: 78 kb
*************Completed test****************
2)1)Click on "Test Local image" button result should be as following
*************Test Local Image****************
Before testing image size is :<---- 78 kb
UIImagePNGRepresentation: image size is---->: 78 kb
UIImageJPEGRepresentation with scale 1.0: image size is---->: 78 kb
*************Completed test****************
Actual Results:
1)Click on "Test Online Image" button result should be as following
*************Test Online Image****************
Before testing image size is :<---- 78 kb
UIImagePNGRepresentation: image size is---->: 480 kb
UIImageJPEGRepresentation with scale 1.0: image size is---->: 180 kb
*************Completed test****************
2)1)Click on "Test Local image" button result should be as following
*************Test Local Image****************
Before testing image size is :<---- 78 kb
UIImagePNGRepresentation: image size is---->: 480 kb
UIImageJPEGRepresentation with scale 1.0: image size is---->: 180 kb
*************Completed test******************
My Question :
why it is increasing its size ? and what is the optimized way to convert image to NSData?
Notes:
Please download "hero_ipad_retina.jpg" image from here and save in your resources
See Question&Answers more detail:
os