ios - 减小 UIImage 大小不起作用

标签 ios iphone

我正在尝试将图像大小减小到小于 64000 字节这是我的代码和日志。

    NSData *beforeData = UIImageJPEGRepresentation(self.photo, 1.0);
    NSLog(@"Size of Image(MB):%u",[beforeData length]/1048576);

    float compression = 0.6;
    NSData* imgData = UIImageJPEGRepresentation(self.photo, compression);
    while ([imgData length] > 400) {
       compression -= 0.1;
        imgData = UIImageJPEGRepresentation(self.photo, compression);
     }

   UIImage *result = [[UIImage alloc] initWithData:imgData];
  NSData *data = UIImageJPEGRepresentation(result, 1.0);
  NSLog(@"Size of Image(MB):%u",[data length]/1048576);

日志:

  • 图片大小(MB):2
  • 图片大小(MB):2

我不知道为什么大小保持不变..也许我需要使用

[data writeToFile:filePath atomically:YES];

最佳答案

由于您只是在循环中更改 JPEG 压缩,因此文件大小会减小。 但是,当您从 JPG 数据创建 UIImage 时,UIImage 的大小并不取决于 JPG 数据的大小。

UIImage 的大小基于高 x 宽 x 4 字节。

关于ios - 减小 UIImage 大小不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22814988/

相关文章:

ios - uilabel sizetofit 不工作 uitableviewcell

ios - Instagram-API 集成认证

ios - 网站无法在 iOS 上正确显示,是否增加了我的视口(viewport)单位?

iphone - 这段代码有什么问题吗?

iphone - 在 iOS 上,为什么旋转后 View 框架的高度几乎保持不变?

ios - 使用 ObjectiveC++ 编译器的问题

iOS 应用内购买永远不会完成

iphone - 如何在使用 AVAudioPlayer 的应用程序的多任务栏中启用音频控件?

iphone - 使用点语法作为 getter 是错误的吗?

ios - NSUserDefaults 还是后端?