標籤:blog http ar io color os 使用 sp for
http://blog.csdn.net/justinjing0612/article/details/8751269
iOS內建的提供了一個API如下
[html] view plaincopy
- 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
- UIImage *imageNew = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
- imageNew = [self imageWithImage:imageNew scaledToSize:CGSizeMake(100, 100)];
- NSData *imageData = UIImageJPEGRepresentation(imageNew, 0.0001);
-
- m_selectImage = [UIImage imageWithData:imageData];
.h具體code
[html] view plaincopy
- #import <Foundation/Foundation.h>
-
- @interface UIImage (UIImageExt)
-
- - (UIImage *)scaleToSize:(UIImage *)img size:(CGSize)size;
-
- - (UIImage *)imageByScalingAndCroppingForSize:(CGSize)targetSize;
- @end
.m具體code
[html] view plaincopy
- #import "UIImageExt.h"
-
-
- @implementation UIImage (UIImageExt)
-
- - (UIImage *)scaleToSize:(UIImage *)img size:(CGSize)size{
- // 建立一個bitmap的context
- // 並把它設定成為當前正在使用的context
- UIGraphicsBeginImageContext(size);
- // 繪製改變大小的圖片
- [img drawInRect:CGRectMake(0, 0, size.width, size.height)];
- // 從當前context中建立一個改變大小後的圖片
- UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
- // 使當前的context出堆棧
- UIGraphicsEndImageContext();
- // 返回新的改變大小後的圖片
- return scaledImage;
- }
-
-
-
- - (UIImage*)imageByScalingAndCroppingForSize:(CGSize)targetSize
- {
- UIImage *sourceImage = self;
- UIImage *newImage = nil;
- CGSize imageSize = sourceImage.size;
- CGFloat width = imageSize.width;
- CGFloat height = imageSize.height;
- CGFloat targetWidth = targetSize.width;
- CGFloat targetHeight = targetSize.height;
- CGFloat scaleFactor = 0.0;
- CGFloat scaledWidth = targetWidth;
- CGFloat scaledHeight = targetHeight;
- CGPoint thumbnailPoint = CGPointMake(0.0,0.0);
-
- if (CGSizeEqualToSize(imageSize, targetSize) == NO)
- {
- CGFloat widthFactor = targetWidth / width;
- CGFloat heightFactor = targetHeight / height;
-
- if (widthFactor > heightFactor)
- scaleFactor = widthFactor; // scale to fit height
- else
- scaleFactor = heightFactor; // scale to fit width
- scaledWidth = width * scaleFactor;
- scaledHeight = height * scaleFactor;
-
- // center the image
- if (widthFactor > heightFactor)
- {
- thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
- }
- else
- if (widthFactor < heightFactor)
- {
- thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
- }
- }
-
- UIGraphicsBeginImageContext(targetSize); // this will crop
-
- CGRect thumbnailRect = CGRectZero;
- thumbnailRect.origin = thumbnailPoint;
- thumbnailRect.size.width = scaledWidth;
- thumbnailRect.size.height = scaledHeight;
-
- [sourceImage drawInRect:thumbnailRect];
-
- newImage = UIGraphicsGetImageFromCurrentImageContext();
- if(newImage == nil)
- NSLog(@"could not scale image");
-
- //pop the context to get back to the default
- UIGraphicsEndImageContext();
- return newImage;
- }
-
- @end
iOS 圖片壓縮UIImage方法擴充