iOS開發探索-圖片壓縮處理

來源:互聯網
上載者:User

標籤:

介紹:

壓: 指檔案體積變小,但是像素數不變,長寬尺寸不變,那麼品質可能下降。
縮: 指檔案的尺寸變小,也就是像素數減少,而長寬尺寸變小,檔案體積同樣會減小。

應用:

在實際開發中,我們經常會對圖片進行處理,滿足開發需求,以下介紹三種圖片壓縮處理:

1.壓縮圖片品質(圖片體積減小,像素不變)

兩種讀取圖片資料的簡單方法:
(1).UIImageJPEGRepresentation函數需要兩個參數:圖片的引用和壓縮係數,壓縮體積不是隨壓縮係數比例變化的。
(2).UIImagePNGRepresentation只需要圖片引用作為參數。
注意:
UIImagePNGRepresentation(image) 要比UIImageJPEGRepresentation(image, 1.0) 返回的圖片資料量大很多
建議優先使用 UIImageJPEGRepresentation ,並可根據自己的實際使用情境,設定壓縮係數,進一步降低圖片資料量大小。 

+(UIImage *)reduceImage:(UIImage *)image percent:(float)percent{   NSData *imageData = UIImageJPEGRepresentation(image, percent);   UIImage *newImage = [UIImage imageWithData:imageData];   return newImage;}

2.圖片壓縮到指定尺寸

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

3.壓縮到指定尺寸等比例壓縮

 -(UIImage *) imageCompressForSize:(UIImage *)sourceImage targetSize:(CGSize)size{    UIImage *newImage = nil;    CGSize imageSize = sourceImage.size;    CGFloat width = imageSize.width;    CGFloat height = imageSize.height;    CGFloat targetWidth = size.width;    CGFloat targetHeight = size.height;    CGFloat scaleFactor = 0.0;    CGFloat scaledWidth = targetWidth;    CGFloat scaledHeight = targetHeight;    CGPoint thumbnailPoint = CGPointMake(0.0, 0.0);    if(CGSizeEqualToSize(imageSize, size) == NO){        CGFloat widthFactor = targetWidth / width;        CGFloat heightFactor = targetHeight / height;        if(widthFactor > heightFactor){            scaleFactor = widthFactor;        }        else{            scaleFactor = heightFactor;        }        scaledWidth = width * scaleFactor;        scaledHeight = height * scaleFactor;        if(widthFactor > heightFactor){            thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;        }else if(widthFactor < heightFactor){        thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;        }    }    UIGraphicsBeginImageContext(size);    CGRect thumbnailRect = CGRectZero;    thumbnailRect.origin = thumbnailPoint;    thumbnailRect.size.width = scaledWidth;    thumbnailRect.size.height = scaledHeight;    [sourceImage drawInRect:thumbnailRect];    newImage = UIGraphicsGetImageFromCurrentImageContext();    if(newImage == nil){        NSLog(@"scale image fail");   }    UIGraphicsEndImageContext();    return newImage;}-(UIImage *) imageCompressForWidth:(UIImage *)sourceImage targetWidth:(CGFloat)defineWidth{    UIImage *newImage = nil;    CGSize imageSize = sourceImage.size;    CGFloat width = imageSize.width;    CGFloat height = imageSize.height;    CGFloat targetWidth = defineWidth;    CGFloat targetHeight = height / (width / targetWidth);    CGSize size = CGSizeMake(targetWidth, targetHeight);    CGFloat scaleFactor = 0.0;    CGFloat scaledWidth = targetWidth;    CGFloat scaledHeight = targetHeight;    CGPoint thumbnailPoint = CGPointMake(0.0, 0.0);    if(CGSizeEqualToSize(imageSize, size) == NO){        CGFloat widthFactor = targetWidth / width;        CGFloat heightFactor = targetHeight / height;    if(widthFactor > heightFactor){        scaleFactor = widthFactor;    }    else{        scaleFactor = heightFactor;    }    scaledWidth = width * scaleFactor;    scaledHeight = height * scaleFactor;    if(widthFactor > heightFactor){        thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;     }else if(widthFactor < heightFactor){        thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;        }    }    UIGraphicsBeginImageContext(size);    CGRect thumbnailRect = CGRectZero;    thumbnailRect.origin = thumbnailPoint;    thumbnailRect.size.width = scaledWidth;    thumbnailRect.size.height = scaledHeight;    [sourceImage drawInRect:thumbnailRect];    newImage = UIGraphicsGetImageFromCurrentImageContext();    if(newImage == nil){       NSLog(@"scale image fail");    }    UIGraphicsEndImageContext();    return newImage;}

iOS開發探索-圖片壓縮處理

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.