Deep Analysis UIImageJPEGRepresentation&UIImagePNGRepresentation

ios中提供了将UIImage转换成NSData的方法

UIKIT_EXTERN NSData *UIImagePNGRepresentation(UIImage *image);                               // return image as PNG. May return nil if image has no CGImageRef or invalid bitmap format
UIKIT_EXTERN NSData *UIImageJPEGRepresentation(UIImage *image, CGFloat compressionQuality);  // return image as JPEG. May return nil if image has no CGImageRef or invalid bitmap format. compression is 0(most)..1(least)

其中UIImageJPEGRepresentation(UIImage *image, CGFloat compressionQuality)提供了一个压缩比率的参数compressionQuality,但是实际体验确实compressionQuality并不能够按照设定好的数值,按比例压缩.

比如一张388kb的图片(jpg格式),通过UIImageJPEGRepresentation方法压缩

compressionQuality = 1.0 压缩后图片大小为979kb;

compressionQuality = 0.5 压缩后图片大小为506kb;

compressionQuality = 0.1 压缩后图片大小为188kb;

compressionQuality压缩系数,跟最后文件的大小并没有明显的关系,不同的图片呈现不同结果.

本人对图片存储格式不是很了解,但是图片颜色细节越单一,图片可压缩的比率会越高.

这就带来一个问题,究竟我们如何设置compressionQuality这个参数.我们如何保证图片的原始大小转成NSData.

如果图片是PNG格式, UIImageJPEGRepresentation压缩率会很高.如果想保证图片原有质量,就需要用到UIImagePNGRepresentation(UIImage *image).

问题1:如何获得图片的原始尺寸,发送原图?

发送图片的时候需要将图片转换成NSData,这时肯定会涉及到UIImageJPEGRepresentation&UIImagePNGRepresentation这两个方法.

如果是jpg的图片,如果用了UIImagePNGRepresentation方法,图片会变得很大.如果用UIImageJPEGRepresentation,又涉及到compressionQuality参数.

如果是png的图片,直接用UIImagePNGRepresentation方法.关键是如何判断图片是PNG格式.

解决方案:可以通过判断图片的格式选择合适转换方法,如果是PNG格式直接用UIImagePNGRepresentation方法,如果是其它格式建议选择用UIImageJPEGRepresentation方法,通过imagepicker获取的图片,可以获得图片的扩展属性,通过这个扩展属性可以判断图片的格式.因为UIImage无法判断源文件的图片格式.

问题2:如何有效的对图片进行压缩?

当使用UIImageJPEGRepresentation方法时,一定要注意compressionQuality参数的设置,设定合理的参数,既保证图片的压缩比率,又能保证图片的清晰度.

除了ios提供的UIImageJPEGRepresentation(UIImage *image, CGFloat compressionQuality)方法,我们还可以通过对图片分辨率的修改,对图片进行压缩

图片大小剪裁:

-(UIImage *) imageCompressForWidth:(UIImage *)sourceImage targetWidth:(CGFloat)defineWidth
{
    CGSize imageSize = sourceImage.size;
    CGFloat width = imageSize.width;
    CGFloat height = imageSize.height;
    CGFloat targetWidth = defineWidth;
    CGFloat targetHeight = (targetWidth / width) * height;
    UIGraphicsBeginImageContext(CGSizeMake(targetWidth, targetHeight));
    [sourceImage drawInRect:CGRectMake(0,0,targetWidth,  targetHeight)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

通过降低图片分辨率的方法,可以极大减小图片的文件大小.

相册ImagePicker获取Image的两种方式:

UIImagePickerControllerOriginalImage:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
        UIImage *orgImage = info[UIImagePickerControllerOriginalImage];
        [picker dismissViewControllerAnimated:YES completion:NULL];
}

UIImagePickerControllerReferenceURL:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
            [library assetForURL:[info objectForKey:UIImagePickerControllerReferenceURL]
                     resultBlock:^(ALAsset *asset)
             {
                 ALAssetRepresentation *representation = [asset defaultRepresentation];
                 CGImageRef imgRef = [representation fullResolutionImage];
                 UIImage *image = [UIImage imageWithCGImage:imgRef
                                                    scale:representation.scale
                                              orientation:(UIImageOrientation)representation.orientation];
                 NSData * data = UIImageJPEGRepresentation(image, 0.5);                

             }failureBlock:^(NSError *error){
                 NSLog(@"couldn‘t get asset: %@", error);
             }
             ];}
时间: 2024-10-10 08:26:29

Deep Analysis UIImageJPEGRepresentation&UIImagePNGRepresentation的相关文章

Deep Analysis Java Memory Model

提纲: •Java内存模型 •volatile关键字 •long和double变量的特殊规则 •原子性,可见性与有序性 •先行发生原则 •Java与线程 1.java内存模型 Java虚拟机规范中试图定义一种java内存模型(Java Memory Model,JMM)来屏蔽掉各种硬件和操作系统的内存访问差异,以实现让Java程序在各种平台上都能达到一致的内存访问效果. 主内存与工作内存: JMM的主要目标是定义了程序中各个变量的访问规则,及虚拟机中将变量存储到内存和从内存中取出变量的底层细节.

iOS一行代码压缩图片大小

现在基本所有应用都与图片相关联,这就必然涉及到上传下载图片,而用户的流量又迟迟没有被解放,因此图片就不能太大,我们知道iPhone一张照片动辄几M,如果都传原图那流量就会爆炸,粗暴地缩小又会影响图片的分辨率.那有没有办法在保持一定分辨率的情况下压缩图片呢?有的,而且非常简单,一行代码搞定,是苹果自带的压缩函数: UIImageJPEGRepresentation UIImagePNGRepresentation 这两个函数都是iOS自带的图片压缩工具.一个是压成JPEG格式,一个是压成PNG格式

iOS 图片转NSData-b

iOS开发中 UIImage可能经常需要转为NSData 上传 传递等等 有两个比较常用的方法 UIImageJPEGRepresentation UIImagePNGRepresentation 第一个方法有两个参数 UIImageJPEGRepresentation(UIImage * __nonnull image, CGFloat compressionQuality); 第一个参数就是图片,第二个参数 就是需要设置的清晰度,值介于0到1之前,值越大却清晰,占用空间就越大,反之,值越小清

Nginx vs Apache

原文地址:https://anturis.com/blog/nginx-vs-apache/ Nginx vs Apache What is the Nginx web and proxy server and how does it compare to Apache? Should you use one of these servers or both? Here we explore some answers to these questions. Nginx web服务器和代理服务器是

2017年浙江中医药大学大学生程序设计竞赛(重现赛)D - CC的神奇背包

题目描述 CC is a smart girl and she is curious about everything. One day she starts to analyze her lifestyle and finds out that she either feels happy or unhappy in any day, then she collects her mood data and makes numerical representation. She writes d

2017 NAIPC A:Pieces of Parentheses

my team solve the problem in the contest with similar ideathis is a more deep analysis The main idea is that if some comparator can be defined so that,if the pieces are previously sorted, always exist some optimal solution that can be formed followin

UIImageJPEGRepresentation和UIImagePNGRepresentation

在Iphone上有两种读取图片数据的简单方法: UIImageJPEGRepresentation和UIImagePNGRepresentation. UIImageJPEGRepresentation函数需要两个参数:图片的引用和压缩系数.而UIImagePNGRepresentation只需要图片引用作为参数.通过在实际使用过程中,比较发现: UIImagePNGRepresentation(UIImage* image) 要比UIImageJPEGRepresentation(UIImag

【CV论文阅读】Deep Linear Discriminative Analysis, ICLR, 2016

DeepLDA 并不是把LDA模型整合到了Deep Network,而是利用LDA来指导模型的训练.从实验结果来看,使用DeepLDA模型最后投影的特征也是很discriminative 的,但是很遗憾没有看到论文是否验证了topmost 的hidden representation 是否也和softmax指导产生的representation一样的discriminative. DeepLDA和一般的deep network唯一不同是它的loss function.两者对比如下: 对于LDA,

A Full Hardware Guide to Deep Learning

A Full Hardware Guide to Deep Learning Deep Learning is very computationally intensive, so you will need a fast CPU with many cores, right? Or is it maybe wasteful to buy a fast CPU? One of the worst things you can do when building a deep learning sy