在Objective-C中缩小图像而不影响质量

7

如何在编程中缩小图像而不影响其质量。

在捕获图像后,我想使用Objective-C减小图像的大小,而不改变其质量。


2
你说的尺寸是指屏幕上的还是磁盘上的? - Sergey Kalinichenko
1
如果我可以捕获3 MB的图像大小,那么在上传到服务器之前,我需要将其减小为500 KB。并且它可以来自任何设备,例如iPhone(5、5S、6)或iPad。 - Arsad
NSData *imageData = UIImageJPEGRepresentation(img, compressionQuality)。如果你Google一下,可以找到很多相关资料! - Ketan Parmar
1
可能是减少iPhone相机/照片库图像文件大小至少为100 KB的重复问题。 - Ketan Parmar
2个回答

11

以下是我用来压缩图片的代码

代码:

-(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。


如何在Objective-C中使Janamejaya先生的图像中心部分变得像曲线形状一样苗条? - Ramani Hitesh
图像部分的中心点瘦形状,适用于Objective-C。 - Ramani Hitesh
@Janmenjaya 图像质量受到影响。 - Yogendra Patel
@YogendraPatel 如果您想保持相同的质量,可以将图像裁剪到所需的尺寸。在这里,我们正在进行缩放和调整大小,因此不会获得原始图像的质量。 - Janmenjaya
@Janmenjaya 选择大小超过5MB的图像,然后压缩它并检查结果。 - Yogendra Patel
@YogendraPatel,正如我之前说过的,由于我们正在缩放,所以图像质量无法与原始质量相同。不管怎样,如果您知道更好的方法,使图像在不影响原始质量的情况下可以压缩,请在此回答。这将对其他人有帮助。 - Janmenjaya

6

我在这个主题上进行了很多搜索。几天后,我找到了这一个。希望它比已接受的答案更快。

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);

它对我非常有效!试试看。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接