【发布时间】:2017-11-10 21:28:47
【问题描述】:
当我使用 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)请从here下载“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 的优化方法是什么?
注意事项: 请从here下载“hero_ipad_retina.jpg”图片并保存在您的资源中
【问题讨论】:
-
哇。我读到的第一个问题看起来像苹果雷达......不知道我对此有何感受。
-
您已经重复了两次本地图像测试...
-
感谢我编辑了我的问题
-
@GovindaraoKondala +1 为您提出问题的方式..我想知道
标签: ios