ios中映像進行壓縮方法匯總

來源:互聯網
上載者:User

   在Iphone上有兩種讀取圖片資料的簡單方法: UIImageJPEGRepresentation和UIImagePNGRepresentation. UIImageJPEGRepresentation函數需要兩個參數:圖片的引用和壓縮係數.而UIImagePNGRepresentation只需要圖片引用作為參數.

  方法一:

  代碼如下:

  - (UIImage*)scaleFromImage:(UIImage*)image scaledToSize:(CGSize)newSize

  {

  CGSize imageSize = image.size;

  CGFloat width = imageSize.width;

  CGFloat height = imageSize.height;

  if (width <= newSize.width && height <= newSize.height){

  return image;

  }

  if (width == 0 || height == 0){

  return image;

  }

  CGFloat widthFactor = newSize.width / width;

  CGFloat heightFactor = newSize.height / height;

  CGFloat scaleFactor = (widthFactor

  CGFloat scaledWidth = width * scaleFactor;

  CGFloat scaledHeight = height * scaleFactor;

  CGSize targetSize = CGSizeMake(scaledWidth,scaledHeight);

  UIGraphicsBeginImageContext(targetSize);

  [image drawInRect:CGRectMake(0,0,scaledWidth,scaledHeight)];

  UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();

  UIGraphicsEndImageContext();

  return newImage;

  }

  方法二:

  .h具體code

   代碼如下:

  #import

  @interface UIImage (UIImageExt)

  - (UIImage *)scaleToSize:(UIImage *)img size:(CGSize)size;

  - (UIImage *)imageByScalingAndCroppingForSize:(CGSize)targetSize;

  @end

  .m具體code

   代碼如下:

  #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

  方法三:(本人項目中使用的方法)

   代碼如下:

  -(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;

  }

  以上所述就是本文的全部內容了,希望大家能夠喜歡。

相關文章

聯繫我們

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