当我使用UIImagePNGRepresentation或UIImageJPEGRepresentation将UIImage转换为NSData时,图像大小增加了很多。
重现步骤:
1)打开Xcode并选择新项目作为基于单个视图的应用程序
2)打开ViewController.xib并添加两个按钮,分别命名为i)Test Online Image ii)Test Local image
3)添加两个IBAction
i) -(IBAction)ClickLocalImageTest:(id)sender;
ii) -(IBAction)ClickOnLineImageTest:(id)sender;
4)将“测试在线图片”连接到“-(IBAction)ClickOnLineImageTest:(id)sender
”,将“测试本地图片”连接到“-(IBAction)ClickLocalImageTest:(id)sender
”;
5)实现以下方法:-(IBAction)ClickLocalImageTest:(id)sender
。
- (IBAction)ClickLocalImageTest:(id)sender {
NSLog(@"*************Test Local Image****************\n");
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 \n",[UIImageJPEGRepresentation(img, 1.0) length]/1024);
NSLog(@"*************Completed test****************\n\n\n\n");
}
6) 将 impalement 方法 "- (IBAction)ClickOnLineImageTest:(id)sender
" 编写如下:
- (IBAction)ClickOnLineImageTest:(id)sender {
NSLog(@"*************Test Online Image****************\n");
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 \n",[UIImageJPEGRepresentation(img, 1.0) length]/1024);
NSLog(@"*************Completed test****************\n\n\n\n");
}
7)请从这里下载"hero_ipad_retina.jpg"图像,并将其命名为"hero_ipad_retina.jpg"保存在您的资源中。
7)现在在Xcode 4.0及以上版本和IOS3.0以上版本的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******************
我的问题:
为什么它的大小在增加?将图像转换为NSData的最佳方法是什么?
注: 请从这里下载“hero_ipad_retina.jpg”图像,并保存到您的资源中。