【问题标题】:the image is not compressed in ios [closed]图像未在 ios 中压缩 [关闭]
【发布时间】:2016-10-03 08:05:51
【问题描述】:

图像未压缩我使用以下代码

UIImage *image = [UIImage imageNamed:@"1.png"];
CGSize newSize = CGSizeMake(100.0f, 100.0f);
UIGraphicsBeginImageContext(newSize);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
_backgroundImageView.image = newImage;

【问题讨论】:

  • 你需要把图片缩小吗?
  • 我需要将图像放在图像视图中。图像视图在故事板中。不是由程序创建的。
  • 我需要将图像放置在图像视图中,但图像视图放置在情节提要中。对于任何模拟器和图像视图,图像都不会失去其独创性......
  • 好的,那么这里的图像未压缩是什么意思?
  • 仍然没有得到你想要做的事情,你让我很困惑。

标签: ios objective-c uiimage


【解决方案1】:

如果要压缩图片

UIImage *img = [UIImage imageNamed:@"yourimagename"];
NSData *postData = UIImageJPEGRepresentation(img, 1.0);
NSInteger imgSizeBytes = [postData length];
double imgSizeKBytes = ceil((double)imgSizeBytes / 1024);
NSString *strBytes;
if(imgSizeKBytes > 1024) {
    double imgSizeMBytes = (double)imgSizeKBytes / 1024;
    strBytes = [NSString stringWithFormat:@"%.1f MB", imgSizeMBytes];
}
else {
    strBytes = [NSString stringWithFormat:@"%d KB", (int)imgSizeKBytes];
}

imageView.image = [UIImage imageWithData:postData scale:0.1];

您可以使用以下代码压缩成所需的图像大小

NSData *postData = UIImageJPEGRepresentation(img, (double)(100-75)/100); 

上面我给了75。如果你想给你的尺寸,给那里。

【讨论】:

    猜你喜欢
    • 2012-09-15
    • 2012-06-13
    • 2019-11-03
    • 2021-02-01
    • 2016-12-19
    • 1970-01-01
    • 2012-06-28
    • 2013-12-28
    • 1970-01-01
    相关资源
    最近更新 更多