- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我有一个用户上传的 UIImage。
如果图像数据超过 10Mb,我如何将它的大小调整到最大 10Mb?到目前为止,我发现的最接近数据大小调整的是:
NSData *imageData = UIImageJPEGRepresentation(theUploadedImage.image, 0.5f);
但我似乎无法控制 Mb 的文件大小...仅控制 JPG 表示中的第二个参数(图像质量 float )
最佳答案
必须创建我自己的函数来尽可能小地压缩图像,如果它仍然超过我的“最大大小”,那么它会调整大小、保留并再次开始压缩迭代。如果图像超过文件大小时,这可以很好地使图像尽可能接近目标“最大图像文件大小”。还包括 1024 次迭代后的故障保护。 (这应该永远不会超过一分钟(但这种情况永远不会发生......谁在 iPhone 上使用千兆字节的图像?哈哈))...
-(void)shrinkImage {
//IMPORTANT!!! THIS CODE WAS CREATED WITH "ARC" IN MIND... DO NOT USE WITHOUT ARC UNLESS YOU ALTER THIS CODE TO MANAGE MEMORY
float compressionVal = 1.0;
float maxVal = 9.7;//MB
UIImage *compressedImage = theUploadedImage.image; //get UIImage from imageView
int iterations = 0;
int totalIterations = 0;
float initialCompressionVal = 0.00000000f;
while (((((float)(UIImageJPEGRepresentation(compressedImage, compressionVal).length))/(1048576.000000000f)) > maxVal) && (totalIterations < 1024)) {
NSLog(@"Image is %f MB", (float)(((float)(UIImageJPEGRepresentation(compressedImage, compressionVal)).length)/(1048576.000000f)));//converts bytes to MB
compressionVal = (((compressionVal)+((compressionVal)*((float)(((float)maxVal)/((float)(((float)(UIImageJPEGRepresentation(compressedImage, compressionVal).length))/(1048576.000000000f)))))))/(2));
compressionVal *= 0.97;//subtracts 3% of it's current value just incase above algorithm limits at just above MaxVal and while loop becomes infinite.
if (initialCompressionVal == 0.00000000f) {
initialCompressionVal = compressionVal;
}
iterations ++;
if ((iterations >= 3) || (compressionVal < 0.1)) {
iterations = 0;
NSLog(@"%f", compressionVal);
compressionVal = 1.0f;
compressedImage = [UIImage imageWithData:UIImageJPEGRepresentation(compressedImage, compressionVal)];
float resizeAmount = 1.0f;
resizeAmount = (resizeAmount+initialCompressionVal)/(2);//percentage
resizeAmount *= 0.97;//3% boost just incase image compression algorithm reaches a limit.
resizeAmount = 1/(resizeAmount);//value
initialCompressionVal = 0.00000000f;
UIView *imageHolder = [[UIView alloc] initWithFrame:CGRectMake(0,0,(int)floorf((float)(compressedImage.size.width/(resizeAmount))), (int)floorf((float)(compressedImage.size.height/(resizeAmount))))];//round down to ensure frame isnt larger than image itself
UIImageView *theResizedImage = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,(int)ceilf((float)(compressedImage.size.width/(resizeAmount))), (int)ceilf((float)(compressedImage.size.height/(resizeAmount))))];//round up to ensure image fits
theResizedImage.image = compressedImage;
[imageHolder addSubview:theResizedImage];
UIGraphicsBeginImageContextWithOptions(CGSizeMake(imageHolder.frame.size.width, imageHolder.frame.size.height), YES, 1.0f);
CGContextRef resize_context = UIGraphicsGetCurrentContext();
[imageHolder.layer renderInContext:resize_context];
compressedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//after 3 compressions, if we still haven't shrunk down to maxVal size, apply the maximum compression we can, then resize the image (90%?), then re-start the process, this time compressing the compressed version of the image we were checking.
NSLog(@"resize");
}
totalIterations ++;
}
if (totalIterations >= 1024) {
NSLog(@"Image was too big, gave up on trying to re-size");//too many iterations failsafe. Gave up on trying to resize.
} else {
NSData *imageData = UIImageJPEGRepresentation(compressedImage, compressionVal);
NSLog(@"FINAL Image is %f MB ... iterations: %i", (float)(((float)imageData.length)/(1048576.000000f)), totalIterations);//converts bytes to MB
theUploadedImage.image = [UIImage imageWithData:imageData];//save new image to UIImageView.
}
}
关于ios - Change NSData Size of a UIImage - Compress And Resize UIImage With a Maximum Megabyte Mb 限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20458558/
fixnums 的“兆字节”方法在哪里定义?据我了解,它不是核心 ruby 方法,因为它仅在 rails 中可用。 ruby -e 'puts 5.megabytes' 在默认的 rails 安装
我有 .csv 文件,其中字段以逗号分隔,行以\n 分隔。在某些行中我有兆字节符号。我想用零替换它以获得(或多或少)正确的字节大小。 我拥有的是 ,2.6 M, 我想要 ,2600000, 示例 20
我有 .csv 文件,其中字段以逗号分隔,行以\n 分隔。在某些行中我有兆字节符号。我想用零替换它以获得(或多或少)正确的字节大小。 我拥有的是 ,2.6 M, 我想要 ,2600000, 示例 20
我收到此警告:“您的配置主分区的可用空间少于 100 MB;为获得最佳效果,Apple 建议增加可用空间量。”。 Screenshot of XCode 我想知道什么是“主分区”,我该如何处理。 提前
我有一个用户上传的 UIImage。 如果图像数据超过 10Mb,我如何将它的大小调整到最大 10Mb?到目前为止,我发现的最接近数据大小调整的是: NSData *imageData = UIIma
/opt/mssql/bin/sqlservr:这个程序需要一台至少有 2000 兆内存的机器 您好,我正在将 .NET Core、Angular、MS SQL 部署到 EC2(Ubuntu)。但是我
我是一名优秀的程序员,十分优秀!