当我使用 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) 穿刺" - (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”图片并保存在您的资源中