【问题标题】:Shrink image without affecting the quality in objective c在不影响目标 c 质量的情况下缩小图像
【发布时间】:2017-02-28 23:10:04
【问题描述】:

如何在不影响质量的情况下以编程方式缩小图像。

捕获图像后,我想在不改变 Objective-c 质量的情况下减小该图像的大小。

【问题讨论】:

  • 你说的是什么尺寸,屏幕上的尺寸还是磁盘上的尺寸?
  • 该图像的大小,如果我可以捕获 3 MB 的图像大小,那么我需要在上传到服务器之前将大小减小为 500 KB。它可以从任何设备获取,例如 iphone(5,5S, 6) 或 ipad。
  • NSData *imageData = UIImageJPEGRepresentation(img, compressionQuality) google 可以找到很多资料!!!

标签: ios objective-c image shrink


【解决方案1】:

这是我用来压缩图像的代码

代码:

-(UIImage *)compressImage:(UIImage *)image{

    NSData *imgData = UIImageJPEGRepresentation(image, 1); //1 it represents the quality of the image.
    NSLog(@"Size of Image(bytes):%ld",(unsigned long)[imgData length]);

    float actualHeight = image.size.height;
    float actualWidth = image.size.width;
    float maxHeight = 600.0;
    float maxWidth = 800.0;
    float imgRatio = actualWidth/actualHeight;
    float maxRatio = maxWidth/maxHeight;
    float compressionQuality = 0.5;//50 percent compression

    if (actualHeight > maxHeight || actualWidth > maxWidth){
        if(imgRatio < maxRatio){
            //adjust width according to maxHeight
            imgRatio = maxHeight / actualHeight;
            actualWidth = imgRatio * actualWidth;
            actualHeight = maxHeight;
        }
        else if(imgRatio > maxRatio){
            //adjust height according to maxWidth
            imgRatio = maxWidth / actualWidth;
            actualHeight = imgRatio * actualHeight;
            actualWidth = maxWidth;
        }
        else{
            actualHeight = maxHeight;
            actualWidth = maxWidth;
        }
    }

    CGRect rect = CGRectMake(0.0, 0.0, actualWidth, actualHeight);
    UIGraphicsBeginImageContext(rect.size);
    [image drawInRect:rect];
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    NSData *imageData = UIImageJPEGRepresentation(img, compressionQuality);
    UIGraphicsEndImageContext();

    NSLog(@"Size of Image(bytes):%ld",(unsigned long)[imageData length]);

    return [UIImage imageWithData:imageData];
}

所以这里是使用上面的代码

UIImage *org = [UIImage imageNamed:@"MacLehose Stage 7 Stunning Natural Sceneries.jpg"];

UIImage *imgCompressed = [self compressImage:org];

如果你想压缩更多

NSData *dataImage = UIImageJPEGRepresentation(imgCompressed, 0.0);

NSLog(@"Size of Image(bytes):%ld",(unsigned long)[dataImage length]);

通过上述方式,我可以将图像从 2 MB 压缩到接近 50 KB。

【讨论】:

  • 如何使janamejaya sir图像中心部分纤细,如目标c的曲线形状
  • 目标 c 的图像部分纤细形状的中心
  • @Janmenjaya 图像质量受到影响
  • @YogendraPatel 如果你想保持相同的质量,你可以将图像裁剪到所需的尺寸,这里我们正在缩放和调整大小,所以不会得到原始图像的质量
  • @Janmenjaya 选择大小超过 5 mb 的图像,然后将其压缩并检查结果
【解决方案2】:

我经常搜索这个主题。几天后,我找到了这个。希望这比公认的答案快得多。

NSData *imageData;
imageData=[[NSData alloc] initWithData:UIImageJPEGRepresentation((chosenImage), 1.0)];

NSLog(@"[before] image size: %lu--", (unsigned long)[imageData length]);

CGFloat scale= (100*1024)/(CGFloat)[imageData length]; // For 100KB. 


UIImage *small_image=[UIImage imageWithCGImage:chosenImage.CGImage scale:scale orientation:chosenImage.imageOrientation];

imageData = UIImageJPEGRepresentation(small_image, scale*1.00);


NSLog(@"[after] image size: %lu:%f", (unsigned long)[imageData length],scale);

它对我很有用!试一次。

【讨论】:

    猜你喜欢
    • 2019-12-13
    • 1970-01-01
    • 2016-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多