iOS 圖片壓縮UIImage方法擴充

來源:互聯網
上載者:User

標籤:blog   http   ar   io   color   os   使用   sp   for   

http://blog.csdn.net/justinjing0612/article/details/8751269

 

iOS內建的提供了一個API如下

 

[html] view plaincopy
  1. NSData *UIImageJPEGRepresentation(UIImage *image, CGFloat compressionQuality);    

 

在Iphone上有兩種讀取圖片資料的簡單方法: UIImageJPEGRepresentation和UIImagePNGRepresentation. UIImageJPEGRepresentation函數需要兩個參數:圖片的引用和壓縮係數.而UIImagePNGRepresentation只需要圖片引用作為參數.通過在實際使用過程中,比較發現: UIImagePNGRepresentation(UIImage* image) 要比UIImageJPEGRepresentation(UIImage* image, 1.0) 返回的圖片資料量大很多.譬如,同樣是讀取網路攝影機拍攝的同樣景色的照片, UIImagePNGRepresentation()返回的資料量大小為199K ,而 UIImageJPEGRepresentation(UIImage* image, 1.0)返回的資料量大小隻為140KB,比前者少了50多KB.如果對圖片的清晰度要求不高,還可以通過設定 UIImageJPEGRepresentation函數的第二個參數,大幅度降低圖片資料量.譬如,剛才拍攝的圖片, 通過調用UIImageJPEGRepresentation(UIImage* image, 1.0)讀取資料時,返回的資料大小為140KB,但更改壓縮係數後,通過調用UIImageJPEGRepresentation(UIImage* image, 0.5)讀取資料時,返回的資料大小隻有11KB多,大大壓縮了圖片的資料量 ,而且從視角角度看,圖片的品質並沒有明顯的降低.因此,在讀取圖片資料內容時,建議優先使用UIImageJPEGRepresentation,並可根據自己的實際使用情境,設定壓縮係數,進一步降低圖片資料量大小。

 

 

[html] view plaincopy
  1. UIImage *imageNew = [info objectForKey:@"UIImagePickerControllerOriginalImage"];  
  2. imageNew = [self imageWithImage:imageNew scaledToSize:CGSizeMake(100, 100)];  
  3. NSData *imageData = UIImageJPEGRepresentation(imageNew, 0.0001);  
  4.   
  5. m_selectImage = [UIImage imageWithData:imageData];  



 

.h具體code

 

[html] view plaincopy
  1. #import <Foundation/Foundation.h>  
  2.   
  3. @interface UIImage (UIImageExt)  
  4.   
  5. - (UIImage *)scaleToSize:(UIImage *)img size:(CGSize)size;  
  6.   
  7. - (UIImage *)imageByScalingAndCroppingForSize:(CGSize)targetSize;  
  8. @end  

.m具體code

 

 

[html] view plaincopy
    1. #import "UIImageExt.h"  
    2.   
    3.   
    4. @implementation UIImage (UIImageExt)  
    5.   
    6. - (UIImage *)scaleToSize:(UIImage *)img size:(CGSize)size{  
    7.     // 建立一個bitmap的context  
    8.     // 並把它設定成為當前正在使用的context  
    9.     UIGraphicsBeginImageContext(size);  
    10.     // 繪製改變大小的圖片  
    11.     [img drawInRect:CGRectMake(0, 0, size.width, size.height)];  
    12.     // 從當前context中建立一個改變大小後的圖片  
    13.     UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();  
    14.     // 使當前的context出堆棧  
    15.     UIGraphicsEndImageContext();  
    16.     // 返回新的改變大小後的圖片  
    17.     return scaledImage;  
    18. }  
    19.   
    20.   
    21.   
    22. - (UIImage*)imageByScalingAndCroppingForSize:(CGSize)targetSize  
    23. {  
    24.     UIImage *sourceImage = self;  
    25.     UIImage *newImage = nil;  
    26.     CGSize imageSize = sourceImage.size;  
    27.     CGFloat width = imageSize.width;  
    28.     CGFloat height = imageSize.height;  
    29.     CGFloat targetWidth = targetSize.width;  
    30.     CGFloat targetHeight = targetSize.height;  
    31.     CGFloat scaleFactor = 0.0;  
    32.     CGFloat scaledWidth = targetWidth;  
    33.     CGFloat scaledHeight = targetHeight;  
    34.     CGPoint thumbnailPoint = CGPointMake(0.0,0.0);  
    35.       
    36.     if (CGSizeEqualToSize(imageSize, targetSize) == NO)  
    37.     {  
    38.         CGFloat widthFactor = targetWidth / width;  
    39.         CGFloat heightFactor = targetHeight / height;  
    40.           
    41.         if (widthFactor > heightFactor)  
    42.             scaleFactor = widthFactor; // scale to fit height  
    43.         else  
    44.             scaleFactor = heightFactor; // scale to fit width  
    45.         scaledWidth  = width * scaleFactor;  
    46.         scaledHeight = height * scaleFactor;  
    47.           
    48.         // center the image  
    49.         if (widthFactor > heightFactor)  
    50.         {  
    51.             thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;  
    52.         }  
    53.         else  
    54.             if (widthFactor < heightFactor)  
    55.             {  
    56.                 thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;  
    57.             }  
    58.     }  
    59.       
    60.     UIGraphicsBeginImageContext(targetSize); // this will crop  
    61.       
    62.     CGRect thumbnailRect = CGRectZero;  
    63.     thumbnailRect.origin = thumbnailPoint;  
    64.     thumbnailRect.size.width  = scaledWidth;  
    65.     thumbnailRect.size.height = scaledHeight;  
    66.       
    67.     [sourceImage drawInRect:thumbnailRect];  
    68.       
    69.     newImage = UIGraphicsGetImageFromCurrentImageContext();  
    70.     if(newImage == nil)  
    71.         NSLog(@"could not scale image");  
    72.       
    73.     //pop the context to get back to the default  
    74.     UIGraphicsEndImageContext();  
    75.     return newImage;  
    76. }  
    77.   
    78. @end  

iOS 圖片壓縮UIImage方法擴充

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.