如何压缩iPhone中的图像? [英] how to compress image in iphone?

查看:102
本文介绍了如何压缩iPhone中的图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从照片库中拍摄图像。我有4-5 mb的大图像,但我想压缩这些图像。因为我需要将这些图像存储在iphone的本地内存中。因为使用更少的内存或获得更少的内存警告我需要压缩这些图像。

I m taking images from photo library.I have large images of 4-5 mb but i want to compress those images.As i need to store those images in local memory of iphone.for using less memory or for getting less memory warning i need to compress those images.

我不知道如何压缩图像和视频。所以我想知道热压缩图像?

I don't know how to compress images and videos.So i want to know hot to compress images?

    UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];

    NSData* data = UIImageJPEGRepresentation(image,1.0);
    NSLog(@"found an image");

    NSString *path = [destinationPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.jpeg", name]];
    [data writeToFile:path atomically:YES]; 

这是保存图像的代码。我不想将整个图像存储得太大。因此,我想将其压缩到更小的尺寸,因为我需要附加多个图像。

This is the code for saving my image. I dont want to store the whole image as its too big. So, I want to compress it to a much smaller size as I'll need to attach multiple images.

感谢您的回复。

推荐答案

您可以为JPEG编码选择较低的质量

You can choose a lower quality for JPEG encoding

NSData* data = UIImageJPEGRepresentation(image, 0.8);

像0.8这样的东西不应该太明显,应该真正改善文件大小。

Something like 0.8 shouldn't be too noticeable, and should really improve file sizes.

除此之外,在使用如下方法进行JPEG表示之前,请考虑调整图像大小:

On top of this, look into resizing the image before making the JPEG representation, using a method like this:

+ (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {
    UIGraphicsBeginImageContext(newSize);
    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();    
    UIGraphicsEndImageContext();
    return newImage;
}

资料来源:调整UIImage大小的最简单方法?

这篇关于如何压缩iPhone中的图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆