媒介
1、確圖片的緊縮的概念:
“壓” 是指文件體積變小,然則像素數不變,長寬尺寸不變,那末質量能夠降低。
“縮” 是指文件的尺寸變小,也就是像素數削減,而長寬尺寸變小,文件體積異樣會減小。
2、圖片壓的處置
關於“壓”的功效,我們可使用UIImageJPEGRepresentation或UIImagePNGRepresentation辦法完成,
如代碼:
//圖片壓
- (void)_imageCompression{
UIImage *image = [UIImage imageNamed:@"HD"];
//第一個參數是圖片對象,第二個參數是壓的系數,其值規模為0~1。
NSData * imageData = UIImageJPEGRepresentation(image, 0.7);
UIImage * newImage = [UIImage imageWithData:imageData];
}
2.1關於PNG和JPEG格局緊縮
UIImageJPEGRepresentation函數須要兩個參數:圖片的援用和緊縮系數而UIImagePNGRepresentation只須要圖片援用作為參數.
UIImagePNGRepresentation(UIImage *image)要比UIImageJPEGRepresentation(UIImage* image, 1.0)前往的圖片數據量年夜許多.
異樣的一張照片, 應用UIImagePNGRepresentation(image)前往的數據量年夜小為200K,而 UIImageJPEGRepresentation(image, 1.0)前往的數據量年夜小只為150K,比前者少了50K.
假如對圖片的清楚度請求不是極高,建議應用UIImageJPEGRepresentation,可以年夜幅度下降圖片數據量.好比,適才拍攝的圖片,經由過程挪用UIImageJPEGRepresentation(image, 1.0)讀取數據時,前往的數據年夜小為140K,但更改緊縮系數為0.5再讀取數據時,前往的數據年夜小只要11K,年夜年夜緊縮了圖片的數據量,並且清楚度並沒有相差若干,圖片的質量並沒有顯著的下降。是以,在讀取圖片數據內容時,建議優先應用UIImageJPEGRepresentation,並可依據本身的現實應用場景,設置緊縮系數,進一步下降圖片數據量年夜小。
提醒:緊縮系數不宜太低,平日是0.3~0.7,太小則能夠會湧現黑邊等。
3、圖片“縮”處置
經由過程[image draWinRect:CGRectMake(0, 0, targetWidth, targetHeight)]可以停止圖片“縮”的功效。
/**
* 圖片緊縮到指定年夜小
* @param targetSize 目的圖片的年夜小
* @param sourceImage 源圖片
* @return 目的圖片
*/
- (UIImage*)imageByScalingAndCroppingForSize:(CGSize)targetSize withSourceImage:(UIImage *)sourceImage
{
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;
}
這個UIImageJPEGRepresentation(image, 0.0),和 UIImagePNGRepresentation(image); 是1的功效。
這個 [sourceImage draWinRect:CGRectMake(0,0,targetWidth, targetHeight)] 是2的功效。
總結
所以,這倆得聯合應用來知足需求,否則你一味的用1,招致,圖片隱約的不可,然則尺寸照樣很年夜。
以上就是在IOS中緊縮圖片處置的具體引見及實例,願望對年夜家進修IOS開辟有所贊助。
【詳解IOS圖片緊縮處置】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!